2015-09-24 42 views
-1

現在我使用的HttpClient它被廢棄了! :-( 所以我想用HttpURLConnection類.. 這裏是我的代碼:使用URLConnection的,而不是HttpClient的與JSONArray

public JSONArray GetDetails(int ID){ 



     // Get HttpResponse Object from url. 
     // Get HttpEntity from Http Response Object 

     HttpEntity httpEntity = null; 

     try 
     { 

      DefaultHttpClient httpClient = new DefaultHttpClient(); // Default HttpClient 
      HttpGet httpGet = new HttpGet(StaticVariables.url_for_details + ID); 

      HttpResponse httpResponse = httpClient.execute(httpGet); 

      httpEntity = httpResponse.getEntity(); 


     } catch (ClientProtocolException e) { 

      // Signals error in http protocol 
      e.printStackTrace(); 

      //Log Errors Here 



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


     // Convert HttpEntity into JSON Array 
     JSONArray jsonArray = null; 

     if (httpEntity != null) { 
      try { 
       String entityResponse = EntityUtils.toString(httpEntity); 

       Log.e("Entity Response : ", entityResponse); 

       jsonArray = new JSONArray(entityResponse); 

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

     return jsonArray; 
    } 

有人可以幫助我使用HttpURLConnection類,而不是HttpClient的,所以我可以用jsonarray此外

+0

你面臨的問題是什麼?或者特別是你不明白/知道阻止你使用HttpUrlConnection? – Djizeus

+0

我不知道在哪個方式我必須使用HttpUrlConnection在我的代碼 – fabiruu111

回答

0

你可以嗎?像這樣做:

public void getData(){ 

     try { 
        URL url = new URL("YOUR_URL"); 
        HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
        String readStream = readStream(con.getInputStream()); 

        System.out.println(readStream); 

     JSONArray arr = new JSONArray(readStream); 
     for (int i = 0; i < arr.length(); i++) { 
      JSONObject object = arr.getJSONObject(i); 
      String age=object.getString("age"); 
     } 

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


} 
      private String readStream(InputStream in) { 
       StringBuilder sb = new StringBuilder(); 
       try (BufferedReader reader = new BufferedReader(new InputStreamReader(in));) { 

        String nextLine = ""; 
        while ((nextLine = reader.readLine()) != null) { 
        sb.append(nextLine); 
        } 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       return sb.toString(); 
       } 

更多參考檢查:

http://developer.android.com/reference/java/net/HttpURLConnection.html

+0

好吧現在我有來自互聯網的所有數據,但現在我想單個項目像:jsonArray.get(「名稱」),你成爲只有命名 – fabiruu111

+0

什麼是你JSON,它張貼在這裏 – Aakash

+0

[{ 「UID」: 「112」, 「UNIQUE_ID」: 「55f44cbe8899e0.90440511」, 「名」: 「AA」, 「FULL_NAME」: 「TT」, 「電子郵件」 : 「[email protected]」, 「年齡」: 「18」, 「about_the_customer」: 「O」, 「encrypted_pa​​ssword」: 「+ BLA」, 「鹽」: 「BLA」, 「created_at」:「2015-09 -12 18時03分10" 秒,‘的updated_at’:空,‘imageName’:‘bla.jpg’}]和例如我只想顯示一個TextView或別的東西 – fabiruu111

0
@TargetApi(Build.VERSION_CODES.KITKAT) 
    public static String getData(String uri) { 
     BufferedReader reader = null; 
     JSONArray jsonArray = null; 

     try { 
      URL url = new URL(uri); 
      HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
      StringBuilder sb = new StringBuilder(); 
      reader = new BufferedReader(new InputStreamReader(con.getInputStream())); 


      String line; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 

      jsonArray = new JSONArray(sb); 
      for (int i = 0; i < jsonArray.length(); i++) { 
       JSONObject jsonObject = jsonArray.getJSONObject(i); 
       String age = jsonObject.getString("age"); 
       Log.e("age", " is" + age); 
      } 

      return sb.toString(); 

     } catch (Exception e) { 
      e.printStackTrace(); 
      return null; 
     } finally { 
      if (reader != null) { 
       try { 
        reader.close(); 
       } catch (IOException e) { 
        e.printStackTrace(); 
        return null; 
       } 
      } 
     } 
    } 

這是我現在的方法,但這與JSONArray將無法正常工作...... 日誌說: org.json.JSONException:不是原始數組:類java.lang.StringBuilder

相關問題