【发布时间】:2018-06-09 01:06:26
【问题描述】:
我在将我的 JSON 对象解析为多个文本视图时遇到问题。
我有一个 TextView1、TextView2 和 TextView3。
我想从以下位置下载数据:“max”、“min”和“average”,并将数据与 TextView1、TextView2 和 TextView3 中的 .settext 同步
我的 JSON 看起来:{"max":15.6,"min":14.05,"average":14.55}
我的代码如下:
public class JSONTask extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
Log.d("Login attempt", connection.toString());
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String finalJson = buffer.toString();
JSONObject parentObject = new JSONObject(finalJson);
String maxPrice = parentObject.getString("max");
return maxPrice;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
onPostExecute:
protected void onPostExecute (String result){
super.onPostExecute(result);
maxPrice.setText(result);
我无法在其他 TextView 中添加更多字符串和同步。这仅适用于一个 TextView。我无法同步多个 TextView。你能帮帮我吗?
【问题讨论】:
-
为什么不返回一个字符串列表?