如何解决Networkonmainthread异常 [英] How to solve Networkonmainthread Exception
本文介绍了如何解决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" />
这篇关于如何解决Networkonmainthread异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文