2016-04-03 9 views
0

Eclipse ADTでJSON解析を行いましたが、完全に実行されていました。しかし、Android Studioで同じコードを使用すると、DefaultHttpClientとそのブロック内のすべての単語にエラーが表示されていました。ここでAndroid Studio 1.5.1のJSON解析

は私のJSONパーサクラスです

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; 

    } 

} 

誰もが、私はBufferedReaderのに渡すことができるJSON文字列を取得するために接続する方法を提案することができますか?

+0

https://stackoverflow.com/questions/32949626/android-m-org-apache-http-entity-fileentity-deprecatedから作成した私たちのPOJOクラスです – CommonsWare

+0

あなたが言ったように、あなたは最高です。しかし、私はすべての新しいPC用のjarファイルをダウンロードする必要がありますか、それはjarファイルをダウンロードせずに行うことができますか? –

+0

私は、あなたが "新しいPC"が何を指しているか知っています。 Android Studioでは、 'build.gradle'ファイルに1行追加し、OkHttp3は開発マシンにダウンロードされます。 OkHttp3のコードは、既に使用している他のライブラリと同様に、プロジェクトに組み込まれ、APKにパッケージ化されます。 – CommonsWare

答えて

0

この仕事にはライブラリを使うことを強くお勧めします。 GSON lib(Googleが開発)またはJackson libを使用できます。リクエストの場合はRetrofit lib(Squareが開発)を使用できます。

0

は、JSONレスポンスのためのPOJOクラスを作成build.gradleに

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

をGsonの依存関係を追加します。

これは、あなたのjsonレスポンス文字列を下のリンクにコピーするだけです。

json to pojo converter

RIGH側の形で自分のクラス名とパッケージ名を与え、そのファイルをダウンロードしてください。

今、私たちのアプリケーションコードに戻ってきます。

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

デモたちはjson to pojo converter