JSON解析为多个TextView

时间:2017-12-28 13:17:33

标签: java android json

我将我的JSON对象解析为多个textview时遇到问题。

我有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中添加更多String和synch。这只适用于一个TextView。我无法同步多个TextView。你能救我吗?

2 个答案:

答案 0 :(得分:0)

你有两个选择。

第一个选项

返回整个json字符串,如

String line = "";
        while ((line = reader.readLine()) != null) {
            buffer.append(line);
        }
        String finalJson = buffer.toString();
        return finalJson;

然后在onPostExecute上处理你的json,如

protected void onPostExecute (String result){
        super.onPostExecute(result);
        JSONObject parentObject = new JSONObject(result);
String maxPrice = parentObject.getString("max");
maxPrice.setText(result);
String minPrice = parentObject.getString("min");
minPrice.setText(result);
String avgPrice = parentObject.getString("avg");
avgPrice.setText(result);
}

第二个选项是

你必须尝试这样

String result = "";
String maxPrice = parentObject.getString("max");
String avgPrice = parentObject.getString("avg");
String minPrice = parentObject.getString("min");
result = maxPrice+","+avgPrice+","+minPrice;
return result;

然后将其拆分为onPostExecute

protected void onPostExecute (String result){
        super.onPostExecute(result);
        String[] separated = result.split(",");
maxPrice.setText(separated[0]);
minPrice.setText(separated[2]);
avgPrice.setText(separated[1]);
}

答案 1 :(得分:0)

1)从finalJson

返回doInBackground

2)getString&amp; setText

中的onPostExecute
JSONObject parentObject = new JSONObject(result);
String maxPrice = parentObject.getString("max");
String minPrice = parentObject.getString("min");
String avg = parentObject.getString("average");
tvMaxPrice.setText(maxPrice);
tvMinPrice.setText(minPrice);
tvAvg.setText(avg);