0

I did JSON parsing on Eclipse ADT and it was running perfectly. But when used the same code on Android Studio it was showing error in DefaultHttpClient and every other word in that block.

Here is my JSON parser class

JSONParser.java

public class JSONParser {

    JSONArray cArray;
    JSONObject jsonObj;
    InputStream is = null;
    String jsonStr = "";
    String urlString;
    Context context;
    String id;
    String name, email, mobile;

    Contacts contacts;
    List < Contacts > contactList;

    public JSONParser() {
        // TODO Auto-generated constructor stub
    }

    public JSONParser(String url) {
        this.urlString = url;
    }

    public List < Contacts > getString() {

        contactList = new ArrayList < Contacts > ();

        try {
            // **Error in this block**

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(urlString);

            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();
        } catch (NullPointerException e) {
            Log.e("NULL DATA", "cant fetch " + e.toString());
        }

        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();
            jsonStr = sb.toString();


        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }


        if (jsonStr != null) {
            try {

                cArray = new JSONArray(jsonStr);

                JSONObject jsonObj = null;

                // looping through All Contacts
                for (int i = 0; i < cArray.length(); i++) {
                    jsonObj = cArray.getJSONObject(i);

                    id = jsonObj.getString("foodno");
                    name = jsonObj.getString("foodname");
                    email = jsonObj.getString("foodtype");

                    /* JSONObject phoneNo = jsonObj.getJSONObject("phone");
                     mobile = phoneNo.getString("mobile");*/

                    contacts = new Contacts(id, name, email);


                    // adding contact to contact list
                    contactList.add(contacts);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (NullPointerException e) {
                e.printStackTrace();
            }

        } else {
            Log.e("JSONParser", "Couldn't get any data from the url");
        }


        return contactList;

    }

}

Can anyone suggest how to connect to get that json string which i can pass to BufferedReader ?

n1nsa1d00
  • 856
  • 9
  • 23
Sharad Chauhan
  • 4,821
  • 2
  • 25
  • 50
  • https://stackoverflow.com/questions/32949626/android-m-org-apache-http-entity-fileentity-deprecated – CommonsWare Apr 03 '16 at 12:37
  • there you said okHttp is best according to you. But for that i have to download jar file for every new PC or it can be done without downloading jar files? – Sharad Chauhan Apr 03 '16 at 12:50
  • I do know what "every new PC" you are referring to. In Android Studio, you add one line to your `build.gradle` file, and OkHttp3 will be downloaded to the development machine. OkHttp3's code will be incorporated into your project and packaged into your APK, just like any other library that you are already using. – CommonsWare Apr 03 '16 at 12:58
  • thanks for the help:) – Sharad Chauhan Apr 03 '16 at 13:03

2 Answers2

0

I strongly recommend you to use a library to to this job. You could use GSON lib (developed by google) or Jackson lib. For the requests you could use Retrofit lib (developed by Square).

heloisasim
  • 4,956
  • 2
  • 27
  • 36
0

add Gson dependency in build.gradle

compile 'com.google.code.gson:gson:2.4'

then create POJO class for json response.

for that just copy your json response string into below link.

json to pojo converter

give your class name and package name in righ side form and download that file.

now come back in our app code.

Demo demo = new Gson().fromJson("JSON STRING",Demo.class);

Demo is our POJO class which we have created from json to pojo converter

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
ErShani
  • 392
  • 2
  • 9