我正在尝试使用此代码获取图片的信息。 我无法获得图片的链接。
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DownloadTask task = new DownloadTask();
task.execute("..."); //I'm not sharing the link here
}
public class DownloadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String result = "";
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while (data != -1) {
char current = (char) data;
result += current;
data = reader.read();
}
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
//问题必须在这里:
JSONObject jsonObject = new JSONObject(result);
String data = jsonObject.getString("data");
String images = jsonObject.getString("images");
String standard_resolution = jsonObject.getString("standard_resolution");
Log.i("Content", data);
JSONArray arr = new JSONArray(standard_resolution);
for (int i = 0; i < arr.length(); i++) {
JSONObject jsonPart = arr.getJSONObject(i);
Log.i("description", jsonPart.getString("url"));
Log.i("description", jsonPart.getString("description"));
//到这里
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
这是网页回复。
{
"data": {
"type": "image",
"users_in_photo": [{
"user": {
"username": "kevin",
"full_name": "Kevin S",
"id": "3",
"profile_picture": "..."
},
"position": {
"x": 0.315,
"y": 0.9111
}
}],
"filter": "Walden",
"tags": [],
"comments": {
"count": 2
},
"caption": null,
"likes": {
"count": 1
},
"link": "http://instagr.am/p/D/",
"user": {
"username": "kevin",
"full_name": "Kevin S",
"profile_picture": "...",
"id": "3"
},
"created_time": "1279340983",
"images": {
"low_resolution": {
"url": "http://distillery.s3.amazonaws.com/media/2010/07/16/4de37e03aa4b4372843a7eb33fa41cad_6.jpg",
"width": 306,
"height": 306
},
"thumbnail": {
"url": "http://distillery.s3.amazonaws.com/media/2010/07/16/4de37e03aa4b4372843a7eb33fa41cad_5.jpg",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "http://distillery.s3.amazonaws.com/media/2010/07/16/4de37e03aa4b4372843a7eb33fa41cad_7.jpg",
"width": 612,
"height": 612
}
},
"id": "3",
"location": null
}
}
答案 0 :(得分:1)
您想要的数据路径中没有数组。只是对象
String resolution = "standard_resolution";
JSONObject jsonObject = new JSONObject(result);
JSONObject images = jsonObject.getJSONObject("data").getJSONObject("images");
String url = images.getJSONObject(resolution).getString("url");
答案 1 :(得分:0)
要从您的JSON获取图像,您需要了解josn结果中可用的JSONObject和JSONArray的数量,并在遵循json结果结构时遵循json结果的树结构,您将获得图像