Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.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的http post请求_Java_Android - Fatal编程技术网

Java android的http post请求

Java android的http post请求,java,android,Java,Android,我想用java为android发送POST请求 我使用以下代码: HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("myUrl"); try { // Add your data List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair

我想用java为android发送POST请求

我使用以下代码:

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("myUrl");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("artist", "Amy Macdonald"));
        nameValuePairs.add(new BasicNameValuePair("title", "Don't Tell Me That It's Over"));
        nameValuePairs.add(new BasicNameValuePair("album", "Don't Tell Me That It's Over"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
我遇到了一个例外,而且这个Web服务肯定能正常工作,

状态:

用作HTTP消息元素的名称/值对参数

由于HTTPPOST不向URL追加属性(比如说),所以它是以实体体的形式追加属性的。您可以有一个简单的基于字符串的实体或基于MIME的实体体


NameValuePair
实现了classed called,您提供了一个参数和一个值(就像HTTP参数一样)。

您要发布到哪个方案?http还是https?您是否使用
ClientConnectionManager
HttpParams
正确设置了客户端?您的日志中有哪些异常

我看到的一些用于发布数据的代码(假设您的客户端设置正确)之间的唯一区别是,我在execute方法中使用了httpContext,如下所示:

httpPost = new HttpPost(urlString);
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
response = httpClient.execute(httpPost, httpContext);
statusCode = response.getStatusLine().getStatusCode();
其中,在构造函数中使用

httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, new BasicCookieStore());
如果您使用的是https,则需要为客户端设置更多信息,以便它能够处理密钥库要求和连接的其他安全方面。

查看本文

它得到了所有类型:httppost、httpget、httppost和文件上传

httpPost = new HttpPost(urlString);
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
response = httpClient.execute(httpPost, httpContext);
statusCode = response.getStatusLine().getStatusCode();
httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, new BasicCookieStore());