0

I am using httpurlconnection and I can't find understandeble way to parse my json. attribute output return this json {"value":"SUCCESS"} but how to parse the json?

code

protected Void doInBackground(String... params) {


    String ue=params[1];

    try {

        //final TextView outputView = (TextView) findViewById(R.id.showOutput);
        URL url = new URL("my url return json ");

        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        String urlParameters = "e="+ue;
        connection.setRequestMethod("POST");
        connection.setRequestProperty("USER-AGENT", "Mozilla/5.0");
        connection.setRequestProperty("ACCEPT-LANGUAGE", "en-US,en;0.5");
        connection.setDoOutput(true);
        DataOutputStream dStream = new DataOutputStream(connection.getOutputStream());
        dStream.writeBytes(urlParameters);
        dStream.flush();
        dStream.close();
        int responseCode = connection.getResponseCode();

        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Post parameters : " + urlParameters);
        System.out.println("Response Code : " + responseCode);

        final StringBuilder output = new StringBuilder("Request URL " + url);


        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line = "";
        StringBuilder responseOutput = new StringBuilder();
        System.out.println("output===============" + br);
        while((line = br.readLine()) != null ) {
            responseOutput.append(line);
        }
        br.close();

        output.append(System.getProperty("line.separator") + "Response " + System.getProperty("line.separator") + System.getProperty("line.separator") + responseOutput.toString());

        register.this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                //output;
                    try {
                        JSONObject json = new JSONObject(output.toString());
                        message = json.optString("value");

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
               Toast.makeText(register.this,"output: "+output, Toast.LENGTH_SHORT).show();
                progress.dismiss();
            }
        });


    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

protected void onPostExecute() {
    progress.dismiss();
}

if there is any dependences that I should to add to gradel.Please, let me know! thank you in advance

Seham
  • 85
  • 9
  • There are libraries that exist that make all the code you have for HTTP calls unnecessary. Two examples are OkHttp or Volley. If you are working just with JSON, then Retrofit is another. – OneCricketeer Apr 02 '16 at 12:24

1 Answers1

0

Simplest way:

try {
        JSONObject json = new JSONObject(output); // output string
        String message = json.optString("value"); // SUCCESS
    } catch (JSONException e) {
        e.printStackTrace();
    }

But I would recommend to use Gson.

Rohit Arya
  • 6,751
  • 1
  • 26
  • 40
  • display error because the output type is StringBuilder. yes please do you have any good tutorial to use Gson library because I am new in java – Seham Apr 02 '16 at 12:25
  • use it like this: `new JSONObject(output.toString());` – Rohit Arya Apr 02 '16 at 12:26
  • I try it and it's return null, while before I use JSONObject is return {"value":"SUCCESS"} ! – Seham Apr 02 '16 at 12:31
  • pass this **string** `{"value":"SUCCESS"}` as parameter to the constructor `new JSONObject("_here_");`. you are doing it wrong otherwise. Post it how are you using it. – Rohit Arya Apr 02 '16 at 12:34
  • yes I post how I used new JSONObject(output.tostring()); – Seham Apr 02 '16 at 12:39
  • can you check the value of `output.toString()` before passing it to JSONObject? – Rohit Arya Apr 02 '16 at 12:41
  • return {"value":"SUCCESS"}. so why after pass it to JSONObject return null? – Seham Apr 02 '16 at 12:44
  • It shouldn't. Are you getting any errors? – Rohit Arya Apr 02 '16 at 13:03
  • no, but return null result . Is the line of the code is effect?( output.append(System.getProperty("line.separator") + "Response " + System.getProperty("line.separator") + System.getProperty("line.separator") + responseOutput.toString()); ) ? – Seham Apr 02 '16 at 16:39
  • yes it does. I told you to print it before setting it to `JSONObject`. Anyway, try it now. – Rohit Arya Apr 02 '16 at 16:48
  • Whatever is the output, just pass it directly to JSONObject, without appending anything to it. – Rohit Arya Apr 02 '16 at 16:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108047/discussion-between-rohit-arya-and-seham). – Rohit Arya Apr 02 '16 at 17:42