0

Android 用のhttp://hc.apache.org/httpcomponents-core-ga/httpcore/examples/org/apache/http/examples/ElementalHttpServer.javaを使用しています。

次のコードを使用して応答を設定します。

HttpResponse getResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, 404, "Not Found");
getResponse.setEntity(new StringEntity(new String("The requested resource " + target + " could not be found due to mismatch!!")));  
conn.sendResponseHeader(getResponse);
conn.sendResponseEntity(getResponse);

Mozilla ポスターまたはブラウザーでの私の応答には、ヘッダー 404 と応答本文があります。

The requested resource  could not be found due to mismatch!!HTTP/1.1 200 OK

HTTP ボディ文字列のみを取得するにはどうすればよいですか? 応答で HTTP/1.1 200 OK が返されるのはなぜですか。私は自分のコードでこれを設定しません。どんな助けでも大歓迎です。

ここに画像の説明を入力

4

3 に答える 3

0

定義したhandle()メソッドに問題がありました。渡された応答を使用する代わりに、要求ごとに新しいHttpResponseを作成していました。

public void handle(HttpRequest request, HttpResponse response, HttpContext context)
于 2013-01-18T09:03:23.507 に答える
0

これはあなたを助けるかもしれません...私response はあなたが必要なもの だと思います

HttpClient client = new DefaultHttpClient();

        HttpResponse httpResponse;

        try {
            httpResponse = client.execute(request);
            responseCode = httpResponse.getStatusLine().getStatusCode();
            message = httpResponse.getStatusLine().getReasonPhrase();

            HttpEntity entity = httpResponse.getEntity();

            if (entity != null) {

                InputStream instream = entity.getContent();
                response = convertStreamToString(instream);

                // Closing the input stream will trigger connection release
                instream.close();
            }

        } catch (ClientProtocolException e) {
            client.getConnectionManager().shutdown();
            e.printStackTrace();
        } catch (IOException e) {
            client.getConnectionManager().shutdown();
            e.printStackTrace();
        }
于 2013-01-15T13:25:54.823 に答える
0

を使用しResponseHandlerます。1 行のコード。これを使用した Android プロジェクトのサンプルについては、こちらこちらを参照してください。

public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.yoursite.com/user");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        ResponseHandler<String> responseHandler=new BasicResponseHandler();
        String responseBody = httpclient.execute(httppost, responseHandler);
        JSONObject response=new JSONObject(responseBody);
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
} 

この投稿と完全な HttpClient の組み合わせをhttp://www.androidsnippets.org/snippets/36/に追加します。

于 2013-01-15T13:12:03.367 に答える