我对一个小型测试项目有疑问。
- >问一个restfull webservice
public String callRestWebService(){
String result="";
System.out.println(".....REST..........");
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(wsURL);
ResponseHandler<String> handler = new BasicResponseHandler();
try {
result = httpclient.execute(request, handler);
System.out.println("..result..."+result);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
httpclient.getConnectionManager().shutdown();
return(result);
} // end callWebService()
此代码没有问题。 在AndroidManifest.xml中添加SdkVersion后
使用-SDK 机器人:的minSdkVersion =&#34; 16&#34; 机器人:targetSdkVersion =&#34; 23&#34;
该应用在行中有例外
result = httpclient.execute(request, handler);
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
at info.rori.androidlunchbox.MainActivity.callRestWebService(MainActivity.java:57)
at info.rori.androidlunchbox.MainActivity.onCreate(MainActivity.java:27)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
... 11 more
问题出在哪里?
此致 麦克劳德
答案 0 :(得分:0)
检查此stackoverflow帖子。它有解决您问题的确切方法