开发者

HTML response from server

I have an app that submits some from data to a local server and as a result the server will send back a JSON. say {status:"success"}

Its working when I run the app in my 2.3 SDK emulator, but installed in a Galaxy Tab (2.2 SDK) the same response is in the form of a 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)开发者_JS百科:     <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>

Edit: Request sending:-

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

            }

can any one tell me whats going wrong?

Happy Coding..!


The Galaxy Tab must be sending different headers to the emulator. Try disabling the expectation header on your HttpPost object.

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

Also see expectation failed in android during photo upload


Just check this line:

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

I am sure you have made a mistake while creating/encoding JSON object on server side.


There is nothing wrong in your Java code. The problem is in the server side. It has happened once to me also, but my server team changed the server and it solved the issue.

In short, HTTP Error 417 indicates there was a problem in the application data transfer, whereby the server did not receive all of the packets it was anticipating (or they were out of order). This error is only thrown by websites that use CheckUpDown. This is an issue with CheckUpDown's servers, so there is little you can do. You may want to inform wallbase.cc's administrator of your issue via their public forum.


It looks like the accept header of your requests states text/html instead of application/json. Change it.


you got error from server that's why this type of data get in response.

Try to handle error at server side, see what type of data server get when you send request from you application, and how server handle it.

EDIT: In my case this works,

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", "");      
               }

and this is the method 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(); 
     } 


Maybe this is a problem with the defaults used by DefaultHttpClient (User-Agent string, strict transfer encoding, etc.)? Try using AndroidHttpClient instead of DefaultHttpClient. Its defaults may be more compatible http://developer.android.com/reference/android/net/http/AndroidHttpClient.html

If that doesn't work, you may need to compare the HTTP headers being sent by the working iPhone, emulator clients to those of the broken Galaxy client and update settings accordingly.


that's because the server replied with the html of an error page - not exactly json-parseable.


alright rahul have you tried any different kind of response handling for the HTTP Post protocol

i use some thing like this.

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

after this is done... you can just insert any Json decoding methodology to retrive the data from the string called resposeString.

Below are the included import files for this implementation. I know there are different for those.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜