我正在尝试从我的Android应用程序访问Web API,我有Url,它应该返回一些XML文件。我在网上找了一些代码,但没有一个可以工作,我可以从VM浏览器访问url而不是应用程序,我设置了访问互联网的所有必要权限。
基本代码:
public class MainActivity extends Activity {
private static final String URL = "http://192.168.15.12/api/values/2";
protected static final String TAG = null;
HttpResponse response3 = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Log.i(TAG, "2");
try {
Log.i(TAG, "7");
Log.i(TAG, "9");
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(URL));
response3 = client.execute(request);
Log.i(TAG, "10");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// return XML
Log.i(TAG, "00");
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
异常:
05-21 14:34:51.462: I/(789): 2
05-21 14:34:51.462: I/(789): 7
05-21 14:34:51.462: I/(789): 9
05-21 14:34:51.552: D/AndroidRuntime(789): Shutting down VM
05-21 14:34:51.552: W/dalvikvm(789): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-21 14:34:51.613: E/AndroidRuntime(789): FATAL EXCEPTION: main
05-21 14:34:51.613: E/AndroidRuntime(789): android.os.NetworkOnMainThreadException
05-21 14:34:51.613: E/AndroidRuntime(789): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
05-21 14:34:51.613: E/AndroidRuntime(789): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
05-21 14:34:51.613: E/AndroidRuntime(789): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
05-21 14:34:51.613: E/AndroidRuntime(789): at libcore.io.IoBridge.connect(IoBridge.java:112)
05-21 14:34:51.613: E/AndroidRuntime(789): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
05-21 14:34:51.613: E/AndroidRuntime(789): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
05-21 14:34:51.613: E/AndroidRuntime(789): at java.net.Socket.connect(Socket.java:842)
05-21 14:34:51.613: E/AndroidRuntime(789): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
05-21 14:34:51.613: E/AndroidRuntime(789): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
05-21 14:34:51.613: E/AndroidRuntime(789): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-21 14:34:51.613: E/AndroidRuntime(789): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-21 14:34:51.613: E/AndroidRuntime(789): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
05-21 14:34:51.613: E/AndroidRuntime(789): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-21 14:34:51.613: E/AndroidRuntime(789): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-21 14:34:51.613: E/AndroidRuntime(789): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-21 14:34:51.613: E/AndroidRuntime(789): at com.example.sms4.MainActivity$1.onClick(MainActivity.java:70)
05-21 14:34:51.613: E/AndroidRuntime(789): at android.view.View.performClick(View.java:4204)
05-21 14:34:51.613: E/AndroidRuntime(789): at android.view.View$PerformClick.run(View.java:17355)
05-21 14:34:51.613: E/AndroidRuntime(789): at android.os.Handler.handleCallback(Handler.java:725)
05-21 14:34:51.613: E/AndroidRuntime(789): at android.os.Handler.dispatchMessage(Handler.java:92)
05-21 14:34:51.613: E/AndroidRuntime(789): at android.os.Looper.loop(Looper.java:137)
05-21 14:34:51.613: E/AndroidRuntime(789): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-21 14:34:51.613: E/AndroidRuntime(789): at java.lang.reflect.Method.invokeNative(Native Method)
05-21 14:34:51.613: E/AndroidRuntime(789): at java.lang.reflect.Method.invoke(Method.java:511)
05-21 14:34:51.613: E/AndroidRuntime(789): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-21 14:34:51.613: E/AndroidRuntime(789): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-21 14:34:51.613: E/AndroidRuntime(789): at dalvik.system.NativeStart.main(Native Method)