httpGet请求错误W/System.err(6388):android.os.NetworkOnMainThreadException [英] httpGet Request error W/System.err(6388): android.os.NetworkOnMainThreadException

查看:81
本文介绍了httpGet请求错误W/System.err(6388):android.os.NetworkOnMainThreadException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的网络服务中获得返回的价值.

I would like to get returned value from my webservice.

我有管理HTTPRequest的班级:

I have my class which Manages the HTTPRequest:

public class RatePromotions 
{
    public RatePromotions() {}
    
    public String executeHttpGet(String url) throws Exception 
    {
        BufferedReader in = null;
        try 
        {
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet();
            request.setURI(new URI(url));
            HttpResponse response = client.execute(request);
            in = new BufferedReader
            (new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) 
            {
                sb.append(line + NL);
            }
            in.close();
            
            String returnedRate = sb.toString();
            System.out.println(returnedRate);
            
            return returnedRate;
        }
        finally 
        {
            if (in != null) 
            {
                try 
                {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

看起来很基础.

然后我的Web服务应该返回我:是或否.

Then my WebService should return me : YES or NO.

我这样使用它:

RatePromotions rating = new RatePromotions();
                String uid = Secure.getString(getContentResolver(),Secure.ANDROID_ID);
                String url = "http://developer.prixo.fr/API/RatePromo?promo="+promofrombdd.getIdentifier()+"&uid="+uid+"&rate="+"1";
                
                try {
                    if (rating.executeHttpGet(url).equals("YES"))
                    {
                        dialog.cancel();
                        Toast.makeText(TabPromotionsSingleItemActivity.this, "Promotion notee.", Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        dialog.cancel();
                        Toast.makeText(TabPromotionsSingleItemActivity.this, "Impossible de prendre en comptre votre vote. " +
                            "Merci de réessayer.", Toast.LENGTH_LONG).show();
                    }
                } catch (Exception e) {
                    dialog.cancel();
                    Toast.makeText(TabPromotionsSingleItemActivity.this, "Impossible de prendre en comptre votre vote. " +
                        "Merci de réessayer.", Toast.LENGTH_LONG).show();
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

但是它只是给我带来了收获..

But it returns me just the catching thing..

查看我的系统错误:

08-21 15:45:45.870: W/System.err(6388): android.os.NetworkOnMainThreadException
08-21 15:45:45.870: W/System.err(6388):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
08-21 15:45:45.870: W/System.err(6388):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
08-21 15:45:45.870: W/System.err(6388):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
08-21 15:45:45.870: W/System.err(6388):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
08-21 15:45:45.875: W/System.err(6388):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
08-21 15:45:45.875: W/System.err(6388):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
08-21 15:45:45.875: W/System.err(6388):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
08-21 15:45:45.875: W/System.err(6388):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
08-21 15:45:45.875: W/System.err(6388):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
08-21 15:45:45.875: W/System.err(6388):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
08-21 15:45:45.875: W/System.err(6388):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
08-21 15:45:45.875: W/System.err(6388):     at com.dev.prixo.webservice.RatePromotions.executeHttpGet(RatePromotions.java:53)
08-21 15:45:45.875: W/System.err(6388):     at com.dev.prixo.TabPromotionsSingleItemActivity$4.onClick(TabPromotionsSingleItemActivity.java:222)
08-21 15:45:45.875: W/System.err(6388):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:168)
08-21 15:45:45.875: W/System.err(6388):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 15:45:45.875: W/System.err(6388):     at android.os.Looper.loop(Looper.java:137)
08-21 15:45:45.875: W/System.err(6388):     at android.app.ActivityThread.main(ActivityThread.java:4517)
08-21 15:45:45.875: W/System.err(6388):     at java.lang.reflect.Method.invokeNative(Native Method)
08-21 15:45:45.875: W/System.err(6388):     at java.lang.reflect.Method.invoke(Method.java:511)
08-21 15:45:45.875: W/System.err(6388):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
08-21 15:45:45.875: W/System.err(6388):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
08-21 15:45:45.875: W/System.err(6388):     at dalvik.system.NativeStart.main(Native Method)

推荐答案

听起来像您正在尝试在UI线程中发出http请求.从Android 3.0开始,您必须将请求放入另一个线程.

Sounds like you are trying to make an http request in the UI thread. Since Android 3.0 you have to put your request in another thread.

请参阅该帖子: HonyComb和DefaultHttpClient

这篇关于httpGet请求错误W/System.err(6388):android.os.NetworkOnMainThreadException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆