0

My url is: http://a.nextput.com/apps/init/4/a/9fe2d2cbaa8332a4633be17b79208181-2y-10-ELVM4HwkaYaCVu6203Zjfus-G/o?aff_id={aff_id}

It contains single object {"success":true}. How to parse this url and store the json data in a variable?

my doInBackground method:

    protected Void doInBackground(Void... unused) {

    String json = "";
    URL url;
    HttpURLConnection connection = null;
    try {
        url =  new URL("http://a.nextput.com/apps/init/4/a/9fe2d2cbaa8332a4633be17b79208181-2y-10-ELVM4HwkaYaCVu6203Zjfus-G/o?aff_id=");
        connection = (HttpURLConnection) url.openConnection();
        InputStream inputStream = connection.getInputStream();
        InputStreamReader reader = new InputStreamReader(inputStream);
        int data = reader.read();

        while (data != -1) {

            char currentChar = (char) data;
            data = reader.read();
            json += currentChar;


        }

    }catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }

    return json;

    JSONObject jsonObject = new JSONObject(json);
    boolean state = jsonObject.getBoolean("success");
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean("state",state);
    editor.commit();
    return null;

}

In return json; it is showing incompatible types and in JSONObject it is showing unhandled exception: org.json.JSONException. How to resolve it?

6
  • there are lots of example avilable of json parsing...plz try first them.. Commented Feb 4, 2016 at 8:53
  • Possible duplicate of Parsing JSON using GET method with Volley in Adnroid Studio Commented Feb 4, 2016 at 8:53
  • @RavindraKushwaha Its not the duplicate of the question you have suggested. I have tried json parsing. But here it is only one object and that too is response. I'm confused how to perform parsing in this url. Please help. Commented Feb 4, 2016 at 9:04
  • what have you tried @himanshutiwari ...?? It simple use like that JsonObject jsonobject = new JsonObject("here is yours response"); and than String sucess= jsonobject.getBoolean("success"); Commented Feb 4, 2016 at 9:05
  • @RavindraKushwaha I have edited the question and showing what I have tried. Its giving some errors. I also mentioned them in the last. Please help me to resolve them. Commented Feb 4, 2016 at 9:21

1 Answer 1

0

Here is the Solution of this -

 // Making HTTP request
       InputStream is = null;
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);//YOUR URL

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
        JSONObject  jObj = new JSONObject(json);
        boolean isSuccess = jObj.getBoolean("success");
        System.out.println("success : " + isSuccess);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }
Sign up to request clarification or add additional context in comments.

2 Comments

In your code it is showing cannot resolve symbol "is"
InputStream is = null;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.