0

我想从我的网络服务中获取返回值。

我有管理 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();
                }
            }
        }
    }
}

它看起来非常基本。

然后我的 WebService 应该返回我:是或否。

我这样使用它:

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)
4

1 回答 1

0

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

看到那个帖子:HonyComb 和 DefaultHttpClient

于 2012-08-21T13:54:42.590 回答