2016-04-03 40 views
0

Ich habe JSON Parsing auf Eclipse ADT und es lief perfekt. Aber wenn derselbe Code in Android Studio verwendet wurde, zeigte er einen Fehler in DefaultHttpClient und jedes zweite Wort in diesem Block.JSON Parsing in Android Studio 1.5.1

Hier ist meine JSON-Parser Klasse

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; 

    } 

} 

Kann jemand empfehlen, wie man das JSON-String verbinden zu bekommen, die ich BufferedReader passieren kann?

+0

https://stackoverflow.com/questions/32949626/android-m-org-apache-http-entity-fileentity-deprecated erstellt haben – CommonsWare

+0

da hast du gesagt, okHttp ist das Beste für dich. Aber dazu muss ich JAR-Datei für jeden neuen PC herunterladen oder es kann ohne Herunterladen von JAR-Dateien getan werden? –

+0

Ich weiß, was "jeder neue PC" Sie beziehen sich auf. In Android Studio fügen Sie eine Zeile zu Ihrer Datei 'build.gradle' hinzu und OkHttp3 wird auf den Entwicklungscomputer heruntergeladen. Der Code von OkHttp3 wird in Ihr Projekt integriert und in Ihr APK gepackt, genau wie jede andere Bibliothek, die Sie bereits verwenden. – CommonsWare

Antwort

0

Ich empfehle dringend, eine Bibliothek für diesen Job zu verwenden. Sie könnten GSON lib (von Google entwickelt) oder Jackson lib verwenden. Für die Anfragen können Sie Retrofit lib (entwickelt von Square) verwenden.

0

Gson Abhängigkeit in build.gradle

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

dann POJO Klasse für Json Antwort hinzufügen erstellen.

dafür kopieren Sie einfach Ihre JSON Antwort Zeichenfolge in den unteren Link.

json to pojo converter

geben Sie Ihren Klassennamen und Paketnamen in righ Seite Form und die Datei herunterladen.

jetzt wieder in unserem App-Code.

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

Demo ist unsere POJO Klasse, die wir von json to pojo converter