【发布时间】:2020-11-09 18:14:34
【问题描述】:
嗨,先生,我已经看到整个堆栈溢出,但没有发现 aswer 请帮助我,我从 api 获取 json 数据,但我无法从中获取一些项目,我多次收到此错误。如果有任何我错过的问题或解决方案,请帮我解决..
private class JsonTask extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(MainActivity.this);
pd.setMessage("Please wait");
pd.setCancelable(false);
pd.show();
}
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
Log.e(TAG, "doInBackground: BUFFER" + reader.read());
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
Log.e(TAG, "> " + line); //here u ll get whole response...... :-)
Log.e(TAG, "doInBackground: length " + line);
}
return buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result1) {
super.onPostExecute(result1);
String result = result1.toString();
if (pd.isShowing()) {
pd.dismiss();
}
Log.e(TAG, "onPostExecute: " + result);
try {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jObject = jArray.getJSONObject(i);
String name = jObject.getString("name");
Log.e(TAG, "onPostExecute: ");
} // End Loop
Log.e(TAG, "onPostExecute: jsion " + jArray.length());
} catch (JSONException e) {
Log.e(TAG, "onPostExecute Error: " + e.toString());
}
}
}
这里是用api调用的函数
new JsonTask().execute("https://api.iextrading.com/1.0/ref-data/symbols");
【问题讨论】:
-
我只想获得每个项目的名称和符号,请帮助我任何身体..
-
这能回答你的问题吗? How to parse JSON in Java
-
先生的问题是,api 只提供 json 数据,而不是像“post”等所示的一些响应,否则我会解决这个问题
-
“posts”:[ {“post_id”:“123456789012_123456789012”,“actor_id”:“1234567890”,“picOfPersonWhoPosted”:“example.com/photo.jpg”,“nameOfPersonWhoPosted”:“Jane Doe”,“ message": "听起来很酷。等不及要看了!", "likesCount": "2", "cmets": [], "timeOfPost": "1234567890" } ]
-
api 不会那样返回
标签: java android json api android-studio