求教android中,在使用HttpPost类的时候,在新建的时候一直都在报错 20
如题,1.StringuriAPI="http://127.0.0.1:8080//TestServelt/test.do";2.HttpPosthttpRequest=...
如题,
1.String uriAPI = "http://127.0.0.1:8080//TestServelt/test.do";
2.HttpPost httpRequest =new HttpPost(uriAPI);
3.(省略)
4.HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);
第2句一直都在报The constructor HttpPost(String) is undefined的错,版本为android2.2,看了一眼导入包,用快捷键默认导入的是org.apache.http.impl.client.DefaultHttpClient但是参考书上导入的是org.apache.http.client.HttpClient,改为参考书上的包后,错误依旧
第4句的execute处报出The method execute(HttpUriRequest) in the type AbstractHttpClient is not applicable for the arguments (HttpPost)的错误,导入包是org.apache.http.HttpResponse
新手,求详细教导,感谢 展开
1.String uriAPI = "http://127.0.0.1:8080//TestServelt/test.do";
2.HttpPost httpRequest =new HttpPost(uriAPI);
3.(省略)
4.HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);
第2句一直都在报The constructor HttpPost(String) is undefined的错,版本为android2.2,看了一眼导入包,用快捷键默认导入的是org.apache.http.impl.client.DefaultHttpClient但是参考书上导入的是org.apache.http.client.HttpClient,改为参考书上的包后,错误依旧
第4句的execute处报出The method execute(HttpUriRequest) in the type AbstractHttpClient is not applicable for the arguments (HttpPost)的错误,导入包是org.apache.http.HttpResponse
新手,求详细教导,感谢 展开
4个回答
展开全部
我自己写的自己在用的一个HttpRequestHelper类
/**
* @param HttpPost
* @return HttpRespone
* Describe 用于发送HttpPost请求
*/
public HttpResponse HttpRequestPost(HttpPost postRequest) throws IOException {
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(postRequest);
return response;
}
/**
* @param String url
* @param List<headers>
* @param List<BasciNameValuePair>
* @return HttpPost
* Describe HttpPost构造的一种重载
*/
public HttpPost BuildHttpPostRequest(String url,List<org.apache.http.Header> headers,List<BasicNameValuePair> postDatas) throws IOException {
HttpPost postRequest = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
for (org.apache.http.Header h : headers) {
postRequest.addHeader(h);
}
for (BasicNameValuePair vp : postDatas) {
nvps.add(vp);
}
postRequest.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
return postRequest;
}
使用的例子
Public HttpResponse Demo(){
List<org.apache.http.Header> headers = new ...
List<BasicNameValuePari> postDatas = new...
headers.add(new BasicHeader("xxx","xxx"));
.
.
.
postDatas.add(new BasicNameValuePair("xxx","xxx"));
.
.
.
HttpRequestHelper helper = new ..
HttpPost post = helper.BuildHttpPostRequest(URL,headers,postDatas);
HttpResponse response = helper.HttpRequestPost(post);
return reponse;
}
/**
* @param HttpPost
* @return HttpRespone
* Describe 用于发送HttpPost请求
*/
public HttpResponse HttpRequestPost(HttpPost postRequest) throws IOException {
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(postRequest);
return response;
}
/**
* @param String url
* @param List<headers>
* @param List<BasciNameValuePair>
* @return HttpPost
* Describe HttpPost构造的一种重载
*/
public HttpPost BuildHttpPostRequest(String url,List<org.apache.http.Header> headers,List<BasicNameValuePair> postDatas) throws IOException {
HttpPost postRequest = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
for (org.apache.http.Header h : headers) {
postRequest.addHeader(h);
}
for (BasicNameValuePair vp : postDatas) {
nvps.add(vp);
}
postRequest.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
return postRequest;
}
使用的例子
Public HttpResponse Demo(){
List<org.apache.http.Header> headers = new ...
List<BasicNameValuePari> postDatas = new...
headers.add(new BasicHeader("xxx","xxx"));
.
.
.
postDatas.add(new BasicNameValuePair("xxx","xxx"));
.
.
.
HttpRequestHelper helper = new ..
HttpPost post = helper.BuildHttpPostRequest(URL,headers,postDatas);
HttpResponse response = helper.HttpRequestPost(post);
return reponse;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
系统已经整合了,你引入报名org.apache.http.client.methods和 org.apache.http.client试试
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-03-28
展开全部
你好 3是什么啊 ?你的错误是运行的时候报的还是你自己写的时候报的啊?如果是自己写的时候那你就换个包试试 如果是运行的时候报的那你导入的包应该没问题的
问下 你有设置请求参数吗?
问下 你有设置请求参数吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
get呢
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询