2011-10-14 27 views
5

データからローカルサーバーにデータを送信するアプリケーションがあります。その結果、サーバーはJSONを返信します。私が2.3 SDKエミュレータでアプリケーションを実行したときに動作し、Galaxy Tab(2.2 SDK)にインストールされたとき、同じ応答がhtmlの形式で表示されます。サーバーからの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

答えて

4

ギャラクシータブは、エミュレータに異なるヘッダーを送信する必要があります。 HttpPostオブジェクトのexpectationヘッダーを無効にしてみてください。ない正確にJSON-解析可能 -

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

また、サーバがエラーページのHTMLと答えたためだexpectation failed in android during photo upload

+0

なぜ投票が下りますか? –

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を試してみてください。アプリケーションからリクエストを送信するときに取得するデータサーバーのタイプと、サーバーでの処理方法を参照してください。

EDIT:私の場合はこの作品、

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(User-Agent文字列、厳格な転送エンコーディングなど)で使用されるデフォルト設定に問題がありますか? DefaultHttpClientの代わりにAndroidHttpClientを使用してください。そのデフォルト値はより互換性がありますhttp://developer.android.com/reference/android/net/http/AndroidHttpClient.html

動作しない場合は、動作中のiPhone、エミュレータクライアントから送信されたHTTPヘッダーと、破損したGalaxyクライアントのHTTPヘッダーを比較し、それに応じて設定を更新する必要があります。

1

を参照してください。

2

application/jsonではなく、text/htmlというリクエストのacceptヘッダーのようです。それを変更。

1

大丈夫ラーフルは、あなたが私はこのようないくつかのことを使用し、HTTPポストプロトコル

ため取り扱い応答のいずれかの異なる種類を試してみました。

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

これが行われた後...resposeStringという文字列からデータを取得するために、Jsonのデコード方法を挿入することができます。

以下は、この実装に含まれるインポートファイルです。私はそれらのために異なっていることを知っています。

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を使用するWebサイトによってのみスローされます。これはCheckUpDownのサーバーで問題になるため、できることはほとんどありません。あなたの公開フォーラムを介してwallbase.ccの管理者に問題を知らせることができます。

関連する問題