如何解决Networkonmainthread异常
问题描述:
我打电话的房地产网站的Web服务,并为我建立了我自己的方法的所有.. 我制定了执行方法中,我将设置我的网址按我的要求如下:
I call a web service of real estate web site and for that i built my own method's and all.. I develop a execute method in which i will set my url as per my requirement as below :
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;
}
}
}
在GET的情况下,我利用发送HTTPGET请求与我想要的网址,但在上主线程异常时网络生成的。
In get case , i send the request using httpget with my desired url but at that time network on main thread exception is generated.
答
在你的AndroidManifest你也可以使用以下方法来跳过错误:
in your AndroidManifest you can also use the following to skip the error:
<uses-sdk android:minSdkVersion="8"
android:targetSdkVersion="8" />