【发布时间】:2011-12-08 06:24:52
【问题描述】:
我调用房地产网站的网络服务,为此我建立了自己的方法和所有.. 我开发了一个执行方法,我将根据我的要求设置我的网址,如下所示:
public void Execute(RequestMethod method) throws Exception
{
switch(method) {
case GET:
{
//add parameters
String combinedParams = "";
if(!params.isEmpty()){
combinedParams += "?";
for(NameValuePair p : params)
{
String paramString = p.getName() + "=" + URLEncoder.encode(p.getValue(),"UTF-8");
if(combinedParams.length() > 1)
{
combinedParams += "&" + paramString;
}
else
{
combinedParams += paramString;
}
}
}
HttpGet request = new HttpGet(url + combinedParams);
//add headers
for(NameValuePair h : headers)
{
request.addHeader(h.getName(), h.getValue());
}
**executeRequest(request, url);** // This throws an exception
break;
}
case POST:
{
HttpPost request = new HttpPost(url);
//add headers
for(NameValuePair h : headers)
{
request.addHeader(h.getName(), h.getValue());
}
if(!params.isEmpty()){
request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
}
executeRequest(request, url);
break;
}
}
}
在获取的情况下,我使用带有我想要的 url 的 httpget 发送请求,但当时会生成主线程上的网络异常。
【问题讨论】:
标签: android