我想从我的网络服务中获得返回值。
我的班级管理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)
答案 0 :(得分:0)
听起来你正试图在UI线程中发出http请求。从Android 3.0开始,您必须将您的请求放在另一个帖子中。