【问题标题】:HTML response from server来自服务器的 HTML 响应
【发布时间】:2011-10-14 04:15:46
【问题描述】:

我有一个应用程序将一些数据提交到本地服务器,因此服务器将发回 JSON。说{状态:“成功”}

当我在我的 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();

            }

谁能告诉我怎么回事?

编码愉快..!

【问题讨论】:

  • 您从服务器收到错误,这就是此类数据得到响应的原因。
  • 尝试在服务器端处理错误,看看当你从你的应用程序发送请求时服务器得到什么类型的数据,以及服务器如何处理它。
  • 在您发送请求的位置显示您的代码。
  • iPhone 设备调用的相同请求行为正常...
  • 你在服务器端检查了吗?还是只有您从服务器获得响应时出现问题?

标签: android json post


【解决方案1】:

Galaxy Tab 必须向模拟器发送不同的标头。尝试禁用 HttpPost 对象上的期望标头。

httppost.getParams().setBooleanParameter( "http.protocol.expect-continue", false )

另见expectation failed in android during photo upload

【讨论】:

    【解决方案2】:

    只需检查这一行:

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

    我确定您在服务器端创建/编码 JSON 对象时犯了一个错误。

    【讨论】:

      【解决方案3】:

      您的 Java 代码没有任何问题。问题出在服务器端。我也发生过一次,但我的服务器团队更改了服务器并解决了问题。

      简而言之,HTTP 错误 417 表明应用程序数据传输存在问题,服务器没有收到它预期的所有数据包(或者它们出现故障)。此错误仅由使用 CheckUpDown 的网站引发。这是 CheckUpDown 服务器的问题,因此您无能为力。您可能想通过他们的公共论坛将您的问题通知 wallbase.cc 的管理员。

      【讨论】:

        【解决方案4】:

        看起来您的请求的接受标头状态为 text/html 而不是 application/json。改变它。

        【讨论】:

          【解决方案5】:

          你得到了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(is);

          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(); 
               } 
          

          【讨论】:

          • 您好 user370305 和 @Paresh Mayani,但是当我在模拟器中运行应用程序时没有遇到同样的错误!
          【解决方案6】:

          也许这是 DefaultHttpClient 使用的默认值(User-Agent 字符串、严格传输编码等)的问题?尝试使用AndroidHttpClient 而不是DefaultHttpClient。它的默认值可能更兼容http://developer.android.com/reference/android/net/http/AndroidHttpClient.html

          如果这不起作用,您可能需要将工作 iPhone、模拟器客户端发送的 HTTP 标头与损坏的 Galaxy 客户端发送的 HTTP 标头进行比较,并相应地更新设置。

          【讨论】:

            【解决方案7】:

            那是因为服务器回复了一个错误页面的 html - 不完全是 json-parseable。

            【讨论】:

              【解决方案8】:

              好吧,你有没有为 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;
              

              【讨论】:

                猜你喜欢
                • 2011-01-26
                • 2021-07-22
                • 2023-04-11
                • 1970-01-01
                • 2018-09-04
                • 2021-12-15
                • 2013-01-02
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多