2011-10-14 130 views
5

我有一個從數據提交一些到本地服務器的應用程序,因此服務器將發回一個JSON。說{狀態:「成功」}來自服務器的HTML響應

它在我的2.3 SDK模擬器中運行應用程序,但安裝在Galaxy Tab(2.2 SDK)時工作,相同的響應是以HTML的形式。

I/RESPONSE(8190): <?xml version="1.0" encoding="utf-8"?> 
I/RESPONSE(8190): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
I/RESPONSE(8190): "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
I/RESPONSE(8190): <html> 
I/RESPONSE(8190): <head> 
I/RESPONSE(8190):  <title>417 Expectation Failed</title> 
I/RESPONSE(8190): </head> 
I/RESPONSE(8190): <body> 
I/RESPONSE(8190):  <h1>Error 417 Expectation Failed</h1> 
I/RESPONSE(8190):  <p>Expectation Failed</p> 
I/RESPONSE(8190):  <h3>Guru Meditation:</h3> 
I/RESPONSE(8190):  <p>XID: 1902486816</p> 
I/RESPONSE(8190):  <hr> 
I/RESPONSE(8190):  <address> 
I/RESPONSE(8190):  <a href="http://www.varnish-cache.org/">Varnish cache server</a> 
I/RESPONSE(8190):  </address> 
I/RESPONSE(8190): </body> 
I/RESPONSE(8190): </html> 
I/RESPONSE(8190): <-- 
I/RESPONSE(8190): 
I/RESPONSE(8190): <?xml version="1.0" encoding="utf-8"?> 
I/RESPONSE(8190): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
I/RESPONSE(8190): "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
I/RESPONSE(8190): <html> 
I/RESPONSE(8190): <head> 
I/RESPONSE(8190):  <title>417 Expectation Failed</title> 
I/RESPONSE(8190): </head> 
I/RESPONSE(8190): <body> 
I/RESPONSE(8190):  <h1>Error 417 Expectation Failed</h1> 
I/RESPONSE(8190):  <p>Expectation Failed</p> 
I/RESPONSE(8190):  <h3>Guru Meditation:</h3> 
I/RESPONSE(8190):  <p>XID: 1902486816</p> 
I/RESPONSE(8190):  <hr> 
I/RESPONSE(8190):  <address> 
I/RESPONSE(8190):  <a href="http://www.varnish-cache.org/">Varnish cache server</a> 
I/RESPONSE(8190):  </address> 
I/RESPONSE(8190): </body> 
I/RESPONSE(8190): </html> 
W/System.err(8190): org.json.JSONException: A JSONObject text must begin with '{' at character 2 of 
W/System.err(8190): <?xml version="1.0" encoding="utf-8"?> 
W/System.err(8190): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
W/System.err(8190): "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
W/System.err(8190): <html> 
W/System.err(8190): <head> 
W/System.err(8190):  <title>417 Expectation Failed</title> 
W/System.err(8190): </head> 
W/System.err(8190): <body> 
W/System.err(8190):  <h1>Error 417 Expectation Failed</h1> 
W/System.err(8190):  <p>Expectation Failed</p> 
W/System.err(8190):  <h3>Guru Meditation:</h3> 
W/System.err(8190):  <p>XID: 1902486816</p> 
W/System.err(8190):  <hr> 
W/System.err(8190):  <address> 
W/System.err(8190):  <a href="http://www.varnish-cache.org/">Varnish cache server</a> 
W/System.err(8190):  </address> 
W/System.err(8190): </body> 
W/System.err(8190): </html> 

編輯: 請求發送: -

  try { 
       HttpClient client = new DefaultHttpClient(); 
       String postURL = GlobalCodes.getBaseurl(); 
       HttpPost post = new HttpPost(postURL); 
       List<NameValuePair> params = new ArrayList<NameValuePair>(); 

        params.add(new BasicNameValuePair("show", 
          "testpost")); 

       post.setEntity(new UrlEncodedFormEntity(params)); 
       HttpResponse responsePOST = client.execute(post); 
       HttpEntity resEntity = responsePOST.getEntity(); 
       String str2 = EntityUtils.toString(resEntity); 
       Log.i("RESPONSE", " <--"); 
       if (resEntity != null) { 
        Log.i("RESPONSE","**"); 
        JSONObject jsonObj = new JSONObject(str2); 
        if (jsonObj.getString("status").equalsIgnoreCase("succuss")) { 
         ..... 
        } else { 
         ...... 
        } 

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

      } 

任何一個可以告訴我什麼問題呢?

快樂編碼..!

+2

你從服務器得到錯誤,這就是爲什麼這種類型的數據得到迴應。 – user370305

+0

嘗試處理服務器端的錯誤,查看從您的應用程序發送請求時服務器獲得的數據類型以及服務器如何處理它。 – user370305

+0

在您發送請求的地方顯示您的代碼。 – user370305

回答

3

只需選中這一行:

W/System.err(8190): org.json.JSONException: A JSONObject text must begin with '{' at character 2 of 

我相信你已經犯了一個錯誤,同時創建服務器端/編碼JSON對象。

1

你得到了error from server這就是爲什麼這種類型的數據得到迴應。

嘗試到handle error at server side,查看從您的應用程序發送請求時服務器獲得的數據類型以及服務器如何處理它。

編輯:在我的情況下工作的,

JSONObject jsonObject = new JSONObject(); 
      jsonObject.put("key1", value1);    
      jsonObject.put("key2", value2); 

JSONArray jArrayParam = new JSONArray();    
      jArrayParam.put(jsonObject);    

List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(); 
        nameValuePair.add(new BasicNameValuePair("bulkdata",jArrayParam.toString()));    

Log.e("bulkdata", jArrayParam.toString());   

HttpClient httpclient = new DefaultHttpClient(); 

HttpPost httppost = new HttpPost("yor remote server url");   
     httppost.addHeader("Content-Type", "application/x-www-form-urlencoded"); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));   

// Execute HTTP Post Request   
HttpResponse response = httpclient.execute(httppost);   

// get response entity   
HttpEntity entity = response.getEntity(); 

      if (entity != null)  
      {    
       InputStream is = entity.getContent();    

       // convert stream to string    
       result = convertStreamToString(is);    
       result = result.replace("\n", "");  
       } 

,這是方法convertStreamToString(是);

public static String convertStreamToString(InputStream is) throws Exception {  
     BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); 
     StringBuilder sb = new StringBuilder();  
     String line = null;  
     while ((line = reader.readLine()) != null) 
      {   
       sb.append(line + "\n");  
       }  
     is.close();  
    return sb.toString(); 
    } 
+0

嗨user370305和@Paresh Mayani,但我沒有得到同樣的錯誤,當我在我的模擬器中運行應用程序! – rahul

1

也許這是與由DefaultHttpClient(用戶代理字符串,嚴格傳輸編碼等)所使用的默認值的問題?嘗試使用AndroidHttpClient而不是DefaultHttpClient。它的默認設置可能更兼容http://developer.android.com/reference/android/net/http/AndroidHttpClient.html

如果這樣做不起作用,您可能需要比較正在運行的iPhone,仿真器客戶端發送到斷開的Galaxy客戶端的HTTP標頭,並相應地更新設置。

1

那是因爲服務器回答一個錯誤頁面的HTML - 不完全的JSON解析的。

2

它看起來像您的請求的接受標題狀態text/html而不是application/json。更改。

1

正常的拉胡爾有沒有嘗試過任何不同類型的響應處理的HTTP Post協議

我用一些這樣的事。

try { 
       String params; 
       String encodedDataLength; 
       connectURL = new URL(_uRL); 

       params = "Blah... Blah... blah; 

       encodedDataLength = "" + params.length(); 


       conn = (HttpURLConnection) connectURL.openConnection(); 


       //conn.setDoInput(true); 
       conn.setDoOutput(true); // signify a post communcation protocol 



       conn.setRequestProperty("User-Agent","mobile"); 
       conn.setRequestProperty("Content-Language", "en-US"); 
       conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
       conn.setRequestProperty("Content-Length", encodedDataLength); 

       //System.out.println("con :" + conn); 
       os = conn.getOutputStream(); 
       os.write(params.getBytes()); 

       os.flush(); 

       // Getting the response code will open the connection, 
       // send the request, and read the HTTP response headers. 
       // The headers are stored until requested. 


       is = conn.getInputStream(); 

       // get session from the cookie 
       String cookie = conn.getHeaderField("cookie"); 
       if (cookie != null) { 
        session = cookie; 

       } 


       // Get the length and process the data 
       BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
       responseString = br.readLine(); 

       //System.out.println(responseString); 



      } catch (Exception e) { 
       //java.lang.System.out.println("http exception: " + e.printStackTrace()); 






      } finally { 
       try { 
        if (is != null) 
         is.close(); 
        if (os != null) 
         os.close(); 
        if (conn != null) 
         conn.disconnect(); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 

完成此操作後...您只需插入任何Json解碼方法即可從名爲resposeString的字符串中檢索數據。

下面是這個實現的包含的導入文件。我知道這些不同。

import java.io.*; 
import java.net.URL; 
import java.net.HttpURLConnection; 
import java.util.zip.GZIPInputStream; 
import java.lang.String; 
3

您的Java代碼沒有任何問題。問題出在服務器端。它也發生過一次,但我的服務器團隊改變了服務器,它解決了這個問題。

總之,HTTP錯誤417表示有一個在應用數據傳輸,由此服務器未收到所有它被期待的分組的問題(或它們無序)。此錯誤僅由使用CheckUpDown的網站引發。這是CheckUpDown服務器的一個問題,所以你可以做的事情很少。您可能需要通過公共論壇告知wallbase.cc的問題管理員。

相關問題