HTTPGET请求错误错误、HTTPGET

2023-09-12 06:01:14 作者:等一句晚安

我想从我的web服务得到返回的值。

我有我的课至极管理HTT prequest:

 公共类RatePromotions
{
    公共RatePromotions(){}

    公共字符串executeHttpGet(字符串URL)抛出异常
    {
        BufferedReader中的= NULL;
        尝试
        {
            HttpClient的客户端=新DefaultHttpClient();
            HTTPGET请求=新HTTPGET();
            request.setURI(新的URI(URL));
            HTT presponse响应= client.execute(要求);
            在=新的BufferedReader
            (新的InputStreamReader(response.getEntity()的getContent()));
            StringBuffer的SB =新的StringBuffer();
            串线=;
            串NL = System.getProperty(line.separator);
            而((行= in.readLine())!= NULL)
            {
                sb.append(行+ NL);
            }
            附寄();

            串returnedRate = sb.toString();
            的System.out.println(returnedRate);

            返回returnedRate;
        }
        最后
        {
            如果(在!= NULL)
            {
                尝试
                {
                    附寄();
                }赶上(IOException异常E){
                    e.printStackTrace();
                }
            }
        }
    }
}
 

它看起来喜欢很基本的。

然后我的WebService应返回我:YES或NO

我用它这样:

  RatePromotions等级=新RatePromotions();
                字符串的uid = Secure.getString(getContentResolver(),Secure.ANDROID_ID);
                字符串URL = "http://developer.prixo.fr/API/RatePromo?promo="+promofrombdd.getIdentifier()+"&uid="+uid+"&rate="+"1";

                尝试 {
                    如果(rating.executeHttpGet(URL).equals(是))
                    {
                        dialog.cancel();
                        Toast.makeText(TabPromotionsSingleItemActivity.this,推广notee,Toast.LENGTH_SHORT).show();
                    }
                    其他
                    {
                        dialog.cancel();
                        Toast.makeText(TabPromotionsSingleItemActivity.this,不可能去prendre连接comptre您的投票。+
                            的Merci德réessayer,Toast.LENGTH_LONG).show();
                    }
                }赶上(例外五){
                    dialog.cancel();
                    Toast.makeText(TabPromotionsSingleItemActivity.this,不可能去prendre连接comptre您的投票。+
                        的Merci德réessayer,Toast.LENGTH_LONG).show();
                    // TODO自动生成的catch块
                    e.printStackTrace();
                }
 

但它返回我只是赶上的事情。

app接口服务器请求为什么会报错307 为什么重复的GET请求变慢了

请参阅我的系统错误:

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

解决方案

听起来像是你正试图使在UI线程的HTTP请求。由于Android 3.0,你必须把在另一个线程您的要求。

请参阅该职位: HonyComb和DefaultHttpClient

I would like to get returned value from my webservice.

I have my class wich 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();
                }
            }
        }
    }
}

It look likes very basic.

Then my WebService should return me : YES or NO.

I'm using it like that:

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..

See my system errors :

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)

解决方案

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.

See that post : HonyComb and DefaultHttpClient