20171025
https://www.chrisblunt.com/android-consuming-a-remote-json-api-with-volley/
///
package jp.co.wata.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
/*
{
"message": "JSON Response OK",
"code": 200,
"images": [
{
"url": "http://assets.chrisblunt.com/wp-content/uploads/2012/12/IMG_20120619_202506-e1356946615784.jpg",
"title": "Hand Sunset"
},
{
"url": "http://assets.chrisblunt.com/wp-content/uploads/2010/01/PC280118.jpg",
"title": "Helicopter Ride"
},
{
"url": "http://assets.chrisblunt.com/wp-content/uploads/2010/08/brew.jpg",
"title": "Brew + Biscuit"
},
{
"url": "http://assets.chrisblunt.com/wp-content/uploads/2013/05/business_cards.jpg",
"title": "2013 Business Cards"
},
{
"url": "http://assets.chrisblunt.com/wp-content/uploads/2012/12/DSCF7504-copy-150x150.jpg",
"title": "Queenstown Lookout"
}
]
}
*/
JSONObject jsonObject = null;
String json = "{\n" +
"\"message\": \"JSON Response OK\",\n" +
"\"code\": 200,\n" +
"\"images\": [\n" +
"{\n" +
"\"url\": \"http://assets.chrisblunt.com/wp-content/uploads/2012/12/IMG_20120619_202506-e1356946615784.jpg\",\n" +
"\"title\": \"Hand Sunset\"\n" +
"},\n" +
"{\n" +
"\"url\": \"http://assets.chrisblunt.com/wp-content/uploads/2010/01/PC280118.jpg\",\n" +
"\"title\": \"Helicopter Ride\"\n" +
"},\n" +
"{\n" +
"\"url\": \"http://assets.chrisblunt.com/wp-content/uploads/2010/08/brew.jpg\",\n" +
"\"title\": \"Brew + Biscuit\"\n" +
"},\n" +
"{\n" +
"\"url\": \"http://assets.chrisblunt.com/wp-content/uploads/2013/05/business_cards.jpg\",\n" +
"\"title\": \"2013 Business Cards\"\n" +
"},\n" +
"{\n" +
"\"url\": \"http://assets.chrisblunt.com/wp-content/uploads/2012/12/DSCF7504-copy-150x150.jpg\",\n" +
"\"title\": \"Queenstown Lookout\"\n" +
"}\n" +
"]\n" +
"}";
try {
jsonObject = new JSONObject(json);
JSONArray jsonImages = jsonObject.getJSONArray("images");
JSONObject jsonImage = jsonImages.getJSONObject(0);
String url = jsonImage.getString("url");
System.out.println(url);
Picasso.with(getApplicationContext())
.load(url)
.into(imageView);
} catch (JSONException e) {
e.printStackTrace();
}
}
}