2013-09-23 36 views
0

林不完全反應做一個簡單的HTTP GET,安卓HTTPGET的BufferedReader

我看到我的結果不完全有效, 什麼即時做錯了什麼?

這裏的代碼:

class GetDocuments extends AsyncTask<URL, Void, Void> { 

    @Override 
    protected Void doInBackground(URL... urls) { 

     Log.d("mensa", "bajando"); 

      //place proper url 
      connect(urls); 

      return null; 
    } 

    public static void connect(URL[] urls) 
    { 

     HttpClient httpclient = new DefaultHttpClient(); 

     // Prepare a request object 
     HttpGet httpget = new HttpGet("http://tiks.document.dev.chocolatecoded.com.au/documents/api/get?type=tree"); 

     // Execute the request 
     HttpResponse response; 
     try { 

      response = httpclient.execute(httpget); 



      // Examine the response status 
      Log.d("mensa",response.getStatusLine().toString()); 

      // Get hold of the response entity 
      HttpEntity entity = response.getEntity(); 
      // If the response does not enclose an entity, there is no need 
      // to worry about connection release 

      if (entity != null) { 

       // A Simple JSON Response Read 
       InputStream instream = entity.getContent(); 
       String result= convertStreamToString(instream); 
       // now you have the string representation of the HTML request 

       Log.d("mensa", "estratagema :: "+result); 

       JSONObject jObject = new JSONObject(result); 

       Log.d("mensa", "resposta jObject::"+jObject); 

       Log.d("mensa", "alive 1"); 

       JSONArray contacts = null; 
       contacts = jObject.getJSONArray("success"); 

       Log.d("mensa", "resposta jObject::"+contacts); 

       Log.d("mensa", "alive"); 

       //instream.close(); 

      } 


     } catch (Exception e) {} 
    } 



     private static String convertStreamToString(InputStream is) { 
     /* 
     * To convert the InputStream to String we use the BufferedReader.readLine() 
     * method. We iterate until the BufferedReader return null which means 
     * there's no more data to read. Each line will appended to a StringBuilder 
     * and returned as String. 
     */ 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder sb = new StringBuilder(); 

     String line = null; 
     try { 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
       Log.d("mensa", "linea ::"+line); 

      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       is.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return sb.toString(); 
    } 
} 

我把它用:

GetDocuments get = new GetDocuments(); 

     URL url = null; 
    try { 
     url = new URL("ftp://mirror.csclub.uwaterloo.ca/index.html"); 
    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 


    //URL url = new URL("http://www.google.es"); 

    get.execute(url); 

編輯1 我指的是不完全作爲被截斷的反應如何?

請注意以下響應的形象字符串被截斷如何,

這是因爲日誌的大小? 但另一個問題是,它不解析?

enter image description here

的感謝!

+0

您可以更多地指定您的INCOMPLETE響應。我試着調試你的代碼並引發JSONException。 09-23 14:04:42.045:E/a(507):org.json.JSONException:值類型爲java.lang.Boolean的成功時爲true無法轉換爲JSONArray – edisonthk

+0

我沒有得到異常,也請參閱編輯檢查截斷字符串的響應,tnx – MaKo

+0

呵呵,我得到了異常,因爲我在你的代碼上做了一些編輯。在你的GetDocuments類連接方法,添加日誌到你的異常,然後你會看到它 – edisonthk

回答

1

我有完全一樣的問題,在過去的幾天。我發現我的代碼通過WiFi工作,但不是3G。換句話說,我排除了所有常見的線程候選人。我還發現,當我在調試器中運行代碼並在client.execute(...)運行10秒後等待(比如說)時。

我的猜測是,

response = httpclient.execute(httpget); 

本身就是一個異步調用時,它的回報慢的部分結果......因此JSON反序列化出錯。

相反,我想這個版本的回調執行的...

try { 
    BasicResponseHandler responseHandler = new BasicResponseHandler(); 
    String json = httpclient.execute(httpget, responseHandler);   
} finally { 
    httpclient.close(); 
} 

突然間所有的工作。如果你不想要一個字符串,或者想要自己的代碼,那麼看看ResponseHandler接口。希望有所幫助。

2

我不知道這是否會解決您的問題,但你可以擺脫你的方法和簡單的使用:

String responseString = EntityUtils.toString(response.getEntity()); 
1

我已經確認這是因爲java字符串的大小限制。我已經通過在響應中添加字符串「abcd」並在logcat中打印了響應字符串來檢查此問題。但結果是沒有添加字符串「abcd」的截斷respose。

也就是說

try { 
BasicResponseHandler responseHandler = new BasicResponseHandler(); 
String json = httpclient.execute(httpget, responseHandler); 
json= json+"abcd"; 
Log.d("Json ResponseString", json); 
} finally { 
httpclient.close(); 
} 

所以我把一個arrayString收集響應。爲了使陣列,我分裂使用我的JSON格式響應「}」

下面的代碼中給出(這是一個工作大約只有)

BasicResponseHandler responseHandler = new BasicResponseHandler(); 
    String[] array=client.execute(request, responseHandler).split("}"); 

然後你就可以在一個JSON對象解析每個對象和json數組與您的自定義類。

如果你有任何其他好的方法來存儲響應,請分享,因爲我爲每個不同的json響應創建自定義方法);。

謝謝

艾爾沙德

1

我不能」直接造成的聲譽評論,但在應對https://stackoverflow.com/a/23247290/4830567我覺得我應該指出的是,一個Java字符串的大小限制爲大約2GB(整數。 MAX_VALUE),所以這不是截斷的原因。

根據https://groups.google.com/d/msg/android-developers/g4YkmrFST6A/z8K3vSdgwEkJ它是logcat有一個大小限制,這就是爲什麼追加「abcd」和在logcat中打印不起作用。字符串本身可能有附加字符。先前的鏈接討論還提到,HTTP協議本身的大小限制有時可能是一個因素,但大多數服務器和客戶端在內部處理這個約束,以便不會將其暴露給用戶。