Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在没有任何参数的情况下在Java/Android中发送POST请求?_Java_Android - Fatal编程技术网

如何在没有任何参数的情况下在Java/Android中发送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

这是我通常用于向API发送POST请求的代码:

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(…
行?(您没有要发送的实体)是的,成功了,谢谢!!!如果您将此作为答案发布,我会接受它