如何解析JsonArray并在Android的Textview上设置值



下面是我的Json响应,当点击Url与解析参数strlogid=101

[
  {
    "earnpoints": 411,
    "type": "C"
  },
  {
    "earnpoints": 1600,
    "type": "G"
  },
  {
    "earnpoints": 13540,
    "type": "I"
  }
]

下面是我的代码显示点在3个不同的textview

private class PointTask extends AsyncTask<String, Void, Boolean> {
        protected Boolean doInBackground(final String... args) {
            JSONParse jParser = new JSONParse();
            HashMap<String, String> hMap = new HashMap<>();
            hMap.put("strlogid", "101");
            JSONArray jsonarray = jParser.getJSONFromUrl(url,hMap);
            // get JSON data from URL
            for (int i = 0; i < jsonarray.length(); i++) {
                try {
                    JSONObject c = (JSONObject) jsonarray.get(i);
                     c_point=c.getString("points");
                     g_point=c.getString("points");
                    i_point=c.getString("points");
                    t_point = cpoint+gpoint+ipoint;
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            return null;
            }
        @Override
        protected void onPostExecute(final Boolean success) {
            txtTotalPoints.setText(t_point);
            txtOwnPoints.setText(g_point);
            txtClubPoints.setText(c_point);
            txtVoucherPoints.setText(i_point);
        }
    }

TotalPoints,OwnPoints,ClubPoints,VoucherPoints分别显示在Textview上

试试这个

protected Boolean doInBackground(final String... args) {
        JSONParse jParser = new JSONParse();
        HashMap<String, String> hMap = new HashMap<>();
        hMap.put("strlogid", "101");
        JSONArray jsonarray = jParser.getJSONFromUrl(url,hMap);
        // get JSON data from URL
        for (int i = 0; i < jsonarray.length(); i++) {
                JSONObject c = (JSONObject) jsonarray.get(i);
                if (c.getString("type").toString().equalsIgnoreCase("C")) {
                    c_point = c.getInt("earnpoints");
                } else if (c.getString("type").toString().equalsIgnoreCase("G")) {
                    g_point = c.getInt("earnpoints");
                } else if (c.getString("type").toString().equalsIgnoreCase("I")) {
                    i_point = c.getInt("earnpoints");
                }
            }
            t_point = cpoint + gpoint + ipoint;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
        }
    @Override
    protected void onPostExecute(final Boolean success) {
        txtTotalPoints.setText(t_point);
        txtOwnPoints.setText(g_point);
        txtClubPoints.setText(c_point);
        txtVoucherPoints.setText(i_point);
    }
}

不要使用async task进行json解析。我建议你用齐射。

这里是链接

http://www.androidhive.info/2014/05/android-working-with-volley-library-1/

@Override
public void onPostSuccess(String result, int id, boolean isSucess) {
    // Log.e("re<><><><>", result);
    try {
        JSONArray ja = new JSONArray(result);
        for (int arr = 0; arr < ja.length(); arr++) {
            JSONObject json1 = ja.getJSONObject(arr);
            Log.e("length", "" + ja.length());
            // Getting JSON Array node
            String earnpoints= json1.getString("earnpoints");
            String type = json1.getString("type");
        }
     catch (JSONException e) {
        e.printStackTrace();
        Toast.makeText(LoginPage.this, "Please try again later", Toast.LENGTH_SHORT).show();
    }
}

最新更新