0

http 応答を取得するためにさまざまな方法を試しましたが、常にメッセージが null になります。Chrome に付属の Advance Rest Client ツールを使用してみました。その中で、成功した応答を取得します。どこが間違っているのかわかりません。

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

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

ここに画像の説明を入力

HttpClient と HttpPost を使用してコードも試しました。しかし、これも機能しません

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

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

4

1 に答える 1

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

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

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

于 2012-05-11T21:20:51.307 に答える