0

我正在尝试向服务器发送 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();

谢谢您的回答

4

3 回答 3

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);
于 2012-10-08T08:15:34.773 回答
0

post.getEntity().getContent().read(); 使用此代码读取您在 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());

于 2012-10-08T08:32:59.327 回答
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();
于 2012-10-08T08:38:51.807 回答