如何在没有任何参数的情况下在Java/Android中发送POST请求?
这是我通常用于向API发送POST请求的代码:如何在没有任何参数的情况下在Java/Android中发送POST请求?,java,android,Java,Android,这是我通常用于向API发送POST请求的代码: if (method == "POST") { // request method is POST // defaultHttpClient HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse httpResponse = httpClient
if (method == "POST") {
// request method is POST
// defaultHttpClient
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
}
到目前为止还不错,但是现在有了这个新的API调用,它应该是POST的,但是没有参数
因此,如果我将
NULL
作为参数传递,我会得到一个NULL指针异常,并且没有新的UrlEncodedFormEntity()
构造函数不接受任何参数,那么我该怎么办?如果在POST中没有要发送的实体,请跳过添加实体的命令
if (method == "POST") {
HttpPost httpPost = new HttpPost(url);
if (params != null)
httpPost.setEntity(new UrlEncodedFormEntity(params));
// else - just nothing
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
inputStream = httpEntity.getContent();
}
您是否尝试过跳过整个
httpPost.setEntity(…
行?(您没有要发送的实体)是的,成功了,谢谢!!!如果您将此作为答案发布,我会接受它