繁体   English   中英

如何在没有任何参数的Java / Android中发送POST请求?

[英]How to send POST request in Java/Android without any params?

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

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();
}

到目前为止一切顺利,但是现在有了这个应该是POST但没有参数的新API调用,因此,如果我将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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM