Search code examples
javaandroidjsonandroidhttpclientjsonobjectrequest

i want to get JSONObject from server using http


I have a problem with some deprecated methods.(httpDefault)

What that i want is simple: i have url (json data) -> send request -> receive jsonObject.

i have no idea. i've already tried some tutorial, but it didn't work for me. ( 1. http://techlovejump.com/android-json-parser-from-url/ 2. https://www.youtube.com/watch?v=Fmo3gDMtp8s&list=PLsoBxH455yoZZeeza9TiG8I9dGP0zz5o9&index=4)

here is my sample code. just working with String data. (not from server data.)

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       /* // url : http://www.jsoneditoronline.org/?id=a31f7f816285e14991e175fccd09a561
        * {
        *   "id":123,
        *   "name":abc,
        *   "status":"ok"
        * }
        */
        final String customJSON = "{\"id\":123,\"name\":abc,\"status\":\"ok\"}";

        try {
            JSONObject jsonObject = new JSONObject(customJSON);
            int id = jsonObject.getInt("id");
            String name = jsonObject.getString("name");
            String status = jsonObject.getString("status");
            Toast.makeText(MainActivity.this, "id #"+id+", name #"+name+", status #"+status, Toast.LENGTH_SHORT).show();

        } catch (JSONException e) { e.printStackTrace(); }
    }
}

please give me your tips.

how do i get JSONObject (all i have just url). url : http://www.jsoneditoronline.org/?id=a31f7f816285e14991e175fccd09a561


Solution

  • import org.json.JSONException;
    import org.json.JSONObject;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
        Context context;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            context = this;
    
    
             new AsyncTask<String, Integer, String>() {
                @Override
                protected String doInBackground(String... params) {
    
                    StringBuilder responseString = new StringBuilder();
                    try {
                        HttpURLConnection urlConnection = (HttpURLConnection) new URL("http://you_url_here").openConnection();
                        urlConnection.setRequestMethod("GET");
                        int responseCode = urlConnection.getResponseCode();
                        if (responseCode == 200){
                            BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
                            String line;
                            while ((line = reader.readLine()) != null) {
                                responseString.append(line);
                            }
                        }
                        urlConnection.disconnect();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return responseString.toString();
                }
    
                @Override
                protected void onPostExecute(String o) {
                    Toast toast = Toast.makeText(context, (CharSequence) o, Toast.LENGTH_SHORT);
                    toast.show();
    
                    /* if your json structure this type then it will work now 
                    * {
                    *   "id":123,
                    *   "name":abc,
                    *   "status":"ok"
                    * }
                    */
                    try {
                        JSONObject jsonObject = new JSONObject(o);
                        int id = jsonObject.getInt("id");
                        String name = jsonObject.getString("name");
                        String status = jsonObject.getString("status");
                        Toast.makeText(MainActivity.this, "id #"+id+", name #"+name+", status #"+status, Toast.LENGTH_SHORT).show();
    
                    } catch (JSONException e) { e.printStackTrace(); }
    
    
                }
            }.execute("");
        }
    }
    

    You may try this. it worked for me, I hope it also work for you.