HTTP POST请求为Android [英] http post request for android
问题描述
我想送POST请求在java中为Android。
I want to send POST request in java for android.
我用这个code:
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
}
问题是,当我使用:
The problem is that when i use :
HttpResponse response = httpclient.execute(httppost);
我得到一个异常,而这个web服务的工作是肯定的,
i get an exception, and this webservice is working for sure,
推荐答案
您张贴到什么计划? HTTP或HTTPS?你有设置客户端正确地将 ClientConnectionManager
和的HttpParams
?什么异常,你在你的日志得怎么样?
What scheme are you posting to? http or https? Have you set up the client correctly with a ClientConnectionManager
and HttpParams
? What exceptions are you getting in your logs?
我一些code我张贴数据之间看到的唯一的区别(假设你的客户是否设置正确)是,我使用的HttpContext在这样执行方法:
The only difference I see between some code I have for posting data (assuming your client is setup correctly) is that I use httpContext in the execute method like this:
httpPost = new HttpPost(urlString);
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
response = httpClient.execute(httpPost, httpContext);
statusCode = response.getStatusLine().getStatusCode();
在上下文是建立在构造与
where the context is setup in the constructor with
httpContext = new BasicHttpContext();
httpContext.setAttribute(ClientContext.COOKIE_STORE, new BasicCookieStore());
如果您使用的是HTTPS,则需要安装客户提供更多信息,以便它可以处理连接的密钥存储要求和其他安全方面的问题。
If you are using https, you'll need to setup the client with more information so it can handle the keystore requirements and other secure aspects of the connection.
这篇关于HTTP POST请求为Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!