I m试图与一个休息站联系,以检索一份JSON文件,但当我在Eclipse emulator提出申请时,该信封一开始就关闭了(“不尽人意的检测”)。 由于向httpClient发出呼吁,似乎已经结束。 执行方法。
主要活动守则如下:
public class TestRestActivity extends Activity {
/** Called when the activity is first created. */
TextView info;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
info = (TextView) findViewById(R.id.debug);
String testUri = "http://www.entireweb.com/xmlquery?pz=MYAPIKEY&ip=IP&q=pizza&n=50&format=json";
// print the uri to the screen - (not seen if rest of code runs)
info.setText(testUri);
HttpClient httpclient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpget = new HttpGet(testUri);
HttpResponse response = null;
try {
// TODO fails on this line
response = httpclient.execute(httpget, localContext);
HttpEntity entity = response.getEntity();
if (entity != null) {
info.setText("got it");
}
} catch (ClientProtocolException e) {
// Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// Auto-generated catch block
e.printStackTrace();
} finally {
}
}
}
我也把网站许可列入我的明文档案:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.john.testRes"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
不太熟悉和淡化发展,而且实际上无法理解造成这一问题的原因。 使用一米的皮.是罚款,并将预期的json文件从我的浏览器中退回。 利用AVD I m可以连接因特网的浏览器,I ve用几台不同的DVD对手。
是否有任何建议引起问题? 感谢。
Edit: added errors
11-22 18:13:14.221: D/AndroidRuntime(556): Shutting down VM
11-22 18:13:14.221: W/dalvikvm(556): threadid=1: thread exiting with uncaught exception (group=0x409951f8)
11-22 18:13:14.251: E/AndroidRuntime(556): FATAL EXCEPTION: main
11-22 18:13:14.251: E/AndroidRuntime(556): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.john.testRes/org.john.testRes.TestRestActivity}: android.os.NetworkOnMainThreadException
11-22 18:13:14.251: E/AndroidRuntime(556): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
11-22 18:13:14.251: E/AndroidRuntime(556): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
11-22 18:13:14.251: E/AndroidRuntime(556): at android.app.ActivityThread.access$600(ActivityThread.java:122)
11-22 18:13:14.251: E/AndroidRuntime(556): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
11-22 18:13:14.251: E/AndroidRuntime(556): at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 18:13:14.251: E/AndroidRuntime(556): at android.os.Looper.loop(Looper.java:137)
11-22 18:13:14.251: E/AndroidRuntime(556): at android.app.ActivityThread.main(ActivityThread.java:4340)
11-22 18:13:14.251: E/AndroidRuntime(556): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 18:13:14.251: E/AndroidRuntime(556): at java.lang.reflect.Method.invoke(Method.java:511)
11-22 18:13:14.251: E/AndroidRuntime(556): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-22 18:13:14.251: E/AndroidRuntime(556): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-22 18:13:14.251: E/AndroidRuntime(556): at dalvik.system.NativeStart.main(Native Method)
11-22 18:13:14.251: E/AndroidRuntime(556): Caused by: android.os.NetworkOnMainThreadException
11-22 18:13:14.251: E/AndroidRuntime(556): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1084)
11-22 18:13:14.251: E/AndroidRuntime(556): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
11-22 18:13:14.251: E/AndroidRuntime(556): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
11-22 18:13:14.251: E/AndroidRuntime(556): at java.net.InetAddress.getAllByName(InetAddress.java:220)
11-22 18:13:14.251: E/AndroidRuntime(556): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
11-22 18:13:14.251: E/AndroidRuntime(556): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-22 18:13:14.251: E/AndroidRuntime(556): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-22 18:13:14.251: E/AndroidRuntime(556): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-22 18:13:14.251: E/AndroidRuntime(556): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-22 18:13:14.251: E/AndroidRuntime(556): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-22 18:13:14.251: E/AndroidRuntime(556): at org.john.testRes.TestRestActivity.onCreate(TestRestActivity.java:41)
11-22 18:13:14.251: E/AndroidRuntime(556): at android.app.Activity.performCreate(Activity.java:4465)
11-22 18:13:14.251: E/AndroidRuntime(556): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-22 18:13:14.251: E/AndroidRuntime(556): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
11-22 18:13:14.251: E/AndroidRuntime(556): ... 11 more
11-22 18:13:48.321: I/Process(556): Sending signal. PID: 556 SIG: 9
11-22 18:14:01.552: D/AndroidRuntime(597): Shutting down VM