显示请求行HttpPost

时间:2012-10-08 08:09:59

标签: android httpclient

我正在尝试将Post请求发送到服务器,它正在向我发送数据错误。我想查看我发送的确切请求行。基本上我在做:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(SERVER);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("data1", data1));
nameValuePairs.add(new BasicNameValuePair("data2", data2));
....
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

(here I want to see my request, something like: 
'data1=data1&data2=data2' http://[server.url] )

HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();

感谢您的回答

3 个答案:

答案 0 :(得分:0)

尝试

String url = "http://serverurl";
for(int i =0;i<nameValuePairs.size;i++)
{
url = url+"?"+nameValuePairs.get(0).getName()+"="+nameValuePairs.get(0).getValue();
}
Log.v("your url is",url);

答案 1 :(得分:0)

post.getEntity()的getContent()读()。; 使用此代码将您在HttpPost中设置的整个实体读取为请求参数。 为了将其打印到日志,请使用此方法将InputStream转换为String

public static String convertStreamToString (InputStream is) {

    BufferedReader reader = new BufferedReader( new InputStreamReader( is ) );
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append( line + "\n" );
        }
    }
    catch (IOException e) {
        Log.d( "IOException", "Error occured during convertString " + e.getMessage() );
        e.printStackTrace();
    }
    finally {
        try {
            is.close();
        }
        catch (IOException e) {
            Log.d( "IOException", "Error occured on closing buffer " + e.getMessage() );
            e.printStackTrace();
        }
    }
    return sb.toString();
}

然后编写Log.d(“RequestEntity”,convertStreamToString(post.getEntity()。getCotent());

答案 2 :(得分:0)

删除你的最后一行:(getEntity)并改为:

HttpResponse response = httpClient.execute(httpPost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

StringBuilder builder = new StringBuilder();
String line = "";
while ((line = reader.readLine()) != null) {
    builder.append(line);
}

Strong html = builder.toString();