2011-02-09 9 views
5

アンドロイドのhttp投稿リクエストのレスポンス本文を取得できません。
私は名前と値のペアでユーザーID、パスワードを言及してアンドロイドのhttp投稿リクエストのレスポンス本文を取得できませんか?

postrequest.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

が、私はSC_OK(200)としての応答を取得していますが、応答本体がnullで実行する上でhttppost要求に入れています。

さらにもう1つの問題は、HTTPGetまたはHTTPPostの要求でheaderを指定してsetHeader()機能を使用している場合、私はBAD REQUEST(404)応答を取得しています。

上記の問題を解決するのに手伝ってください。 &よろしく、
SSuman185

+1

あなたが呼び出して応答を読むコードを入れてください。 – Aliostad

+1

サーバーが実際に応答を送信したことを確認しましたか? – sstn

+0

サーバは当社のサードパーティのサーバにはありません。私はBAD Requestを取得しています。ヘッダーとネットワークログから、返信がサーバーのみであることが分かりました。 – Suman

答えて

0

こんにちは友人は、PLサーバーからの応答を取得するには、この方法を試してみてください

おかげ..

考えると、リンクuがリクエストを投稿したいリンク..です

String link2 = "http://184.106.227.45/quaddeals/university-of-illinois/androids_deals/user_card_list.json"; 

DefaultHttpClient hc1 = new DefaultHttpClient(); 

ResponseHandler<String> res1 = new BasicResponseHandler(); 

HttpPost postMethod1 = new HttpPost(link2); 

List<NameValuePair> nameValuePairs1 = new ArrayList<NameValuePair>(1); 

nameValuePairs1.add(new BasicNameValuePair("user_id",account_userId)); 

postMethod1.setEntity(new UrlEncodedFormEntity(nameValuePairs1)); 

String response1 = hc1.execute(postMethod1, res1); 
+0

こんにちはVenkatesh、実際に私はHttpClientとclient.execute()を使用していますが、私たちは私の会社でプロキシを使用しているので、ターゲットではなくシミュレータではうまく動作しています。まあ、まだ運がない – Suman

1

ここでは、ページへのPOSTを簡単に行い、レスポンスを文字列として取得する方法を示します。最初のパラメータ(HashMap)は、送信するパラメータのキー値リストです。エラー処理がないので、それを行う必要があります:

public static String doPost(HashMap<String,String> params, String url) { 
     HttpPost httpost = new HttpPost(url); 
     try { 
      httpost.setURI(new URI(url)); 
     } catch (URISyntaxException e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     HttpResponse response = null; 

     List <NameValuePair> nvps = new ArrayList <NameValuePair>(); 

     Set entryset = params.entrySet(); 
     Iterator iterator = entryset.iterator(); 
     Log.d("Posted URL: ", url+" "); 
     while(iterator.hasNext()) { 
      Map.Entry mapentry = (Map.Entry)iterator.next(); 
      String key = ((String)mapentry.getKey()).toString(); 
      String value = (String)mapentry.getValue(); 
      nvps.add(new BasicNameValuePair(key, value)); 

      Log.d("Posted param: ", key+" = "+value); 
     } 

     try { 
      httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); 
      try { 
       httpost.setURI(new URI(url)); 
      } catch (URISyntaxException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      response = getClient().execute(httpost); 
      HttpEntity entity = response.getEntity(); 

      return convertStreamToString(entity.getContent()); 
     } catch (Exception e) { 
      // some connection error occurred, nothing we can do 
      e.printStackTrace(); 
     } 

     return null; 
    } 
関連する問題