2012-05-11 26 views
0

httpレスポンスを得るためにさまざまな方法を試しましたが、常にメッセージがヌルになっています。私は、クロムに付属のアドバンスレストクライアントツールを使ってみました。その中で私は成功の応答を得る。私はどこで間違っているのか分からない。HttpURLConnection、HttpClient、およびHttpPostリクエストがAndroidのPOSTリクエストで機能しない

try { 

     String urlParameters = "id=userid&password=password&device=android"; 

     URL url = new URL("my url"); 
     HttpURLConnection urlConnection = (HttpURLConnection) url 
       .openConnection(); 

     urlConnection.setRequestMethod("POST"); 
     urlConnection.setRequestProperty("Content-Type", 
       "application/x-www-form-urlencoded"); 

     urlConnection.setRequestProperty("Content-Length", 
       "" + Integer.toString(urlParameters.getBytes().length)); 
     urlConnection.setRequestProperty("Content-Language", "en-US"); 

     urlConnection.setUseCaches(false); 
     urlConnection.setDoInput(true); 
     urlConnection.setDoOutput(true); 

     OutputStream outputStream = urlConnection.getOutputStream(); 
     outputStream.write(urlParameters.getBytes()); 
     outputStream.close(); 

     int responseCode = urlConnection.getResponseCode(); 
     if (responseCode == HttpURLConnection.HTTP_OK) { 
      InputStream in = urlConnection.getInputStream(); 
      resultstring = convertinputStreamToString(in); 
      Log.d("Result String-------->", resultstring); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

私は私が結果を得るスクリーンショット添付した:成功 をしかし、上記のコードでは、私は結果を得る:失敗

enter image description here

は、私はまた、HttpClientをしてHttpPostを使用してコードを試してみました。しかし、これもdoent仕事

try { 
     HttpClient client = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(
       "my url"); 

     httpPost.setHeader("Accept", "application/json"); 
     httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); 

     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3); 
     nameValuePairs.add(new BasicNameValuePair("id", "userid")); 
     nameValuePairs.add(new BasicNameValuePair("password", "password")); 
     nameValuePairs.add(new BasicNameValuePair("device", "android")); 

     // httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     UrlEncodedFormEntity se = new UrlEncodedFormEntity(nameValuePairs); 
     se.setContentType("application/x-www-form-urlencoded"); 
     httpPost.setEntity(se); 
     httpPost.getParams().setBooleanParameter(
       "http.protocol.expect-continue", false); 

     HttpResponse response = client.execute(httpPost); 

     String responseAsText = EntityUtils.toString(response.getEntity()); 
     Log.d("Result String-------->", responseAsText); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

私を助けてください。私はほとんどすべてを試しました。

答えて

-1
HttpClient localHttpClient = this.app2.getHttpClient(); 
    HttpPost localHttpPost = new HttpPost("yourURL.com"); 
    localHttpPost.setEntity(new UrlEncodedFormEntity(myArrayList)); 
    InputStream localInputStream = localHttpClient.execute(localHttpPost).getEntity().getContent(); 

このようなものを試してください。 応答を入力ストリームとして読み取ってから、結果が返された後に作業を行う必要があります。

また、期待どおりに応答を返さない可能性があるため、必ずサーバーを確認してください。

関連する問題