Java 带正文的Http请求

Java 带正文的Http请求,java,android,json,http-post,Java,Android,Json,Http Post,我需要创造 POST请求url为 使用HTTP正文authparams={“login”:“login”,“password”:“pasword”} 如何创建它?我试着 HttpClient httpClient = new DefaultHttpClient(); // Creating HTTP Post HttpPost httpPost = new HttpPost("http://url.com/api/auth/"); // Buil

我需要创造 POST请求url为 使用HTTP正文authparams={“login”:“login”,“password”:“pasword”} 如何创建它?我试着

 HttpClient httpClient = new DefaultHttpClient();
        // Creating HTTP Post
        HttpPost httpPost = new HttpPost("http://url.com/api/auth/");
        // Building post parameters, key and value pair
        List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
        nameValuePair.add(new BasicNameValuePair("login", "A@asd.ru"));
        nameValuePair.add(new BasicNameValuePair("password", "123"));
        // Url Encoding the POST parameters
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
        } catch (UnsupportedEncodingException e) {
            // writing error to Log
            e.printStackTrace();
        }
        // Making HTTP Request
        try {
            HttpResponse response = httpClient.execute(httpPost);
            // writing response to log
            Log.d("myLogs:", response.toString());
            HttpEntity entity = response.getEntity();
            Log.d("myLogs:",EntityUtils.toString(entity) );
        } catch (ClientProtocolException e) {
            // writing exception to log
            e.printStackTrace();
        } catch (IOException e) {
            // writing exception to log
            e.printStackTrace();

        }
HttpClient-HttpClient=newdefaulthttpclient();
//创建HTTP Post
HttpPost HttpPost=新的HttpPost(“http://url.com/api/auth/");
//构建post参数、键和值对
List nameValuePair=新的ArrayList(2);
添加(新的BasicNameValuePair(“登录”)A@asd.ru"));
添加(新的BasicNameValuePair(“密码”,“123”));
//对POST参数进行Url编码
试一试{
setEntity(新的UrlEncodedFormEntity(nameValuePair));
}捕获(不支持的编码异常e){
//将错误写入日志
e、 printStackTrace();
}
//发出HTTP请求
试一试{
HttpResponse response=httpClient.execute(httpPost);
//将响应写入日志
Log.d(“myLogs:,response.toString());
HttpEntity=response.getEntity();
Log.d(“myLogs:,EntityUtils.toString(entity));
}捕获(客户端协议例外e){
//将异常写入日志
e、 printStackTrace();
}捕获(IOE异常){
//将异常写入日志
e、 printStackTrace();
}

但是我得到了一个不好的结果

您需要将post数据转换为Json格式(您可以使用google library gson或使用org.Json)

然后将json字符串添加到post数据中
nameValuePair.add(新的BasicNameValuePair(“登录”)A@asd.ru)

我认为您误解了您发布的代码。这只是向服务器发送POST请求,但当前不处理来自服务器的响应。为此,您可以使用如下代码:

InputStream ips;
ips = response.getEntity().getContent();
final BufferedReader buf = new BufferedReader(new InputStreamReader(ips, "UTF-8"));

StringBuilder sb = new StringBuilder();
String s;
while (true) {
  s = buf.readLine();
  if ((s == null) || (s.length() == 0))
    break;
  sb.append(s);
}

buf.close();
ips.close();
Log.i("Response", "My response is: " + sb.toString());

我得到0,但必须得到1。您得到
0
,其中?a如果我只加载urlLog.d(“myLogs:,EntityUtils.toString(entity)),则得到相同的结果;从服务器获取令牌=0你能解释一下吗?我想我在这里得到了respond HttpResponse response=httpClient.execute(httpPost);实际上是这样,但你必须处理它。您可以通过我包含的代码片段实现这一点(我已经更新了它,请检查它),我的响应是:{“token”:“0”}但是如果我的创建主体使用正确的方式,它必须返回token:1Doesn't
EntityUtils.toString(entity)
实现上述功能??