httpGet请求错误W/System.err(6388):android.os.NetworkOnMainThreadException

httpGet请求错误W/System.err(6388):android.os.NetworkOnMainThreadException,android,android-activity,http-get,Android,Android Activity,Http Get,我想从我的Web服务中获取返回值 我有一个管理HTTPRequest的类: public class RatePromotions { public RatePromotions() {} public String executeHttpGet(String url) throws Exception { BufferedReader in = null; try { HttpCli

我想从我的Web服务中获取返回值

我有一个管理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服务应该返回我:是或否

我是这样使用它的:

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();
                }
但它给了我一个吸引人的东西

请参阅我的系统错误:

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请求。自从安卓3.0以来,你必须将你的请求放在另一个线程中


请参阅该帖子:

您是否在Android manifest.xml中插入了internet权限?是的,UI线程正在阻塞..但是我如何才能从onCreate UI中删除它(以我为例)?也许下面链接的第一个示例可以帮助您。关键是创建一个密钥,并将http请求放入其中。我用的方法就像第一个例子!!非常感谢你