简体   繁体   中英

How to obtain json reponse from http request?

When trying to retrieve the response json from my api I can only ever get a string value which I am unable to convert to jsonobject to use.

Should respond with json format {"result":"success","client[id]":"1"}

But I am only able to retrieve string "result=success,client[id]=1"

public void createWHMCSUser(final String emailID, final String random, final String uid) {

    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                String e = URLEncoder.encode(emailID, "utf-8");
                String r = URLEncoder.encode(random, "utf-8");
                URL url = new URL("http://create.user.com/includes/api.php?action=AddClient&username=abc123&password=abc123&accesskey=abc123&firstname=User&lastname=Name&email=" + e +"&address1=na&city=na&state=na&poscode=00000&country=US&phonenumber=0000000000&password2=" + r + "&reponsetype=json");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Content-Type", "application/json");
                conn.setRequestProperty("Accept","application/json");
                conn.setDoOutput(true);
                conn.setDoInput(true);



                Log.i("STATUS", String.valueOf(conn.getResponseCode()));
                Log.i("MSG", conn.getResponseMessage());

                if(conn.getResponseCode() == 200){
                    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

                    String readAPIResponse = " ";
                    StringBuilder jsonString = new StringBuilder();
                    while((readAPIResponse = in.readLine()) != null){
                        jsonString.append(readAPIResponse);

                        JSONObject obj = new JSONObject(jsonString.toString());
                        int aJsonString = obj.getInt("Client[id]");
                        SetClientID(uid,aJsonString);



                    }

                    in.close();


                }

                conn.disconnect();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    });

    thread.start();

}

use this way more easier and fast for more examples and to built library check this: enter link description here

 AndroidNetworking.get("URL")
                     .setTag("test")
                     .setPriority(Priority.MEDIUM)
                     .build()
                     .getAsJSONObject(new JSONObjectRequestListener() {
                        @Override
                        public void onResponse(JSONObject response) {
                          // handle your response here 

                        }
                        @Override
                        public void onError(ANError error) {
                          // handle error
                        }
                    });

Use the json library to add the java object to JSONObject. That will store all the java object content in to json string format in your server code. That will convert In to json format.

Then you will return the code from server as the regular string.

In the client side, you will receive it as the string.

Then you need to convert it to java object from the json string. That is the actual json string. You will e able to convert that string to java obj.

But what you have return is not the json String. That is just string only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM