2011-11-14 46 views
1

我讀到HttpsURLConnection在2.3之前是buggy,我有第一次體驗它是越野車。Android HttpsURLConnection vs HttpClient,都給出了問題

,我試圖交替是HttpClient的,但它給我的問題,在某種意義上說,它返回我:

<?xml version="1.0" encoding="iso-8859-1"?> 

時,我清楚應該找回JSON。

HttpsURLConnection可以很好地返回JSON,但在處理很多請求時會產生一些不一致的結果。 (通過返回JSON,我的意思是響應應該包含JSON)

任何想法爲什麼HttpClient回饋一些XML標記而不是JSON(應該返回什麼)?

編輯 - CODE

HttpClient client = new DefaultHttpClient(); 
    HttpPost post = new HttpPost(url); 

    List<NameValuePair> urlParams = new ArrayList<NameValuePair>(); 
     urlParams.add(new BasicNameValuePair("&platform=","Android")); 
     urlParams.add(new BasicNameValuePair("&action=","signin")); 
     urlParams.add(new BasicNameValuePair("&user",username)); 
     urlParams.add(new BasicNameValuePair("&pass",password)); 



    try { 

     post.setEntity(new UrlEncodedFormEntity(urlParams, HTTP.UTF_8)); 
     HttpResponse resp = client.execute(post); 
     BufferedReader rd = new BufferedReader(new InputStreamReader(
       resp.getEntity().getContent())); 

     String line = ""; 
     JSONArray ja = new JSONArray(); 
     while ((line = rd.readLine()) != null) { 
      Log.i("------>", line); 
      ja = new JSONArray(line); //breaks here because it returns xml instead of JSON 

     } 
     rd.close(); 
     return ja; 

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

是否有這兩個更好交替?

+1

我們需要一些代碼編寫。 –

+0

我不知道它的問題,但該網站是https,而不是http – volk

+0

首先告訴我它是.net服務或PHP? –

回答

1

這看起來像來自服務器的錯誤頁面的響應。您可能會收到404錯誤或許多其他http錯誤。這只是您從服務器獲取的html文件的開始。你確定這個網址是一個有效的網址,並且會接受來自手機的連接而不重定向嗎?

+0

是的,這個問題,它使用HttpsURLConnection完全正常,並且只有它返回的那一行。就是那個xml位。 – volk

+0

你可以發佈你的電話代碼嗎? – Bobbake4

+0

更新代碼 – volk

0

相反的:

List<NameValuePair> urlParams = new ArrayList<NameValuePair>(); 
     urlParams.add(new BasicNameValuePair("&platform=","Android")); 
     urlParams.add(new BasicNameValuePair("&action=","signin")); 
     urlParams.add(new BasicNameValuePair("&user",username)); 
     urlParams.add(new BasicNameValuePair("&pass",password)); 

試試這個:

List<NameValuePair> urlParams = new ArrayList<NameValuePair>(); 
     urlParams.add(new BasicNameValuePair("platform","Android")); 
     urlParams.add(new BasicNameValuePair("action","signin")); 
     urlParams.add(new BasicNameValuePair("user",username)); 
     urlParams.add(new BasicNameValuePair("pass",password)); 

更新1:

究竟是你想實現在此:

String line = ""; 
     JSONArray ja = new JSONArray(); 
     while ((line = rd.readLine()) != null) { 
      Log.i("------>", line); 
      ja = new JSONArray(line); //breaks here because it returns xml instead of JSON 

     } 

這明顯給錯誤,因爲您嘗試從XML響應字符串創建JSONArray,intsead檢查此功能並實施它。

+0

不幸的是,沒有工作:( – volk

+0

@lazeR任何迴應?顯示logcat輸出或響應,最好是確定一下。 –

+0

響應只有一行:<?xml version =「1.0」encoding =「iso-8859-1」?>並且錯誤爲nullpointerexceptions,因爲JSON數組由於XML不是JSON格式而生成 – volk

1

您應該從BasicNameValuePair key中刪除"&"字符。

BasicNameValuePair autometically附加在每個參數"&"所以沒有必要添加"&"字符

你有如下

urlParams.add(new BasicNameValuePair("platform","Android"));