-2

Below is my Json response from getting when Hit on Url with parsing parameter strlogid=101

[

  {

    "earnpoints": 411,
    "type": "C"
  },

  {

    "earnpoints": 1600,
    "type": "G"
  },

  {

    "earnpoints": 13540,
    "type": "I"
  }

]

Below is my code-To show Points on 3 different 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 are show on Textview respectively

Android Surya
  • 544
  • 3
  • 17

3 Answers3

0

Dont use async task for json parsing. I would reccomend you to use volley.

Here is the link\

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

Rohit Sharma
  • 83
  • 10
  • The OP does not mention any network Operation, It might just be a JSON coming from an input file or related. – Enzokie Aug 11 '16 at 07:35
  • While the link is helpful, it may eventually break, at SO they recommend that you extract the most important parts from the link in question and post them inside your answer, perhaps as a quote, to ensure that your answer doesn't become significantly less useful in the future. – Troyseph Aug 11 '16 at 09:04
0

Try this

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

    }
}
Divyesh Boda
  • 258
  • 2
  • 12
  • There is a lot of controversy surrounding code only answers, perhaps you could explain why your code is the best answer to the question? [Controversy](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers), [more](http://meta.stackexchange.com/questions/95470/down-vote-code-only-answers), [yet more](http://meta.stackexchange.com/questions/140040/how-to-improve-low-quality-answers-consisting-of-only-a-code-block). Good luck! – Troyseph Aug 11 '16 at 09:01
-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();
    }
}
Troyseph
  • 4,960
  • 3
  • 38
  • 61
  • Welcome to StackOverflow, [please consider taking the tour](http://stackoverflow.com/tour), there is a lot of controversy surrounding code only answers, perhaps you could explain why your code is the best answer to the question? [Controversy](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers), [more](http://meta.stackexchange.com/questions/95470/down-vote-code-only-answers), [yet more](http://meta.stackexchange.com/questions/140040/how-to-improve-low-quality-answers-consisting-of-only-a-code-block). Good luck! – Troyseph Aug 11 '16 at 09:00