【问题标题】:JSON Parsing to multiple TextView [duplicate]JSON解析到多个TextView [重复]
【发布时间】: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。你能帮帮我吗?

【问题讨论】:

  • 为什么不返回一个字符串列表?

标签: java android json


【解决方案1】:

1) 从doInBackground返回您的finalJson

2) getString & setTextonPostExecute

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);

【讨论】:

    【解决方案2】:

    你有两个选择。

    第一个选项

    像这样返回整个 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]);
    }
    

    【讨论】:

      猜你喜欢
      • 2013-02-02
      • 1970-01-01
      • 2012-11-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-10
      相关资源
      最近更新 更多