我正在创建一个分配项目,我是 Android 新手,我想从非常常见的 url 访问 json http://api.androidhive.info/contacts/
,
问题:我正在尝试读取 url 并获取并解析此 url 返回的 json,
我已经在我的 AndroidManifest.xml 中添加了以下行
<uses-permission android:name="android.permission.INTERNET"/>
偏好:我的安卓偏好是
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
- API 18 级
- 安卓 4.3
这就是我试图阅读网址的方式
static InputStream is = null;
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
错误信息
11-02 05:23:47.843: E/AndroidRuntime(2207): FATAL EXCEPTION: main
11-02 05:23:47.843: E/AndroidRuntime(2207): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.me.countrypedia/com.me.countrypedia.MainActivity}: android.os.NetworkOnMainThreadException
11-02 05:23:47.843: E/AndroidRuntime(2207): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-02 05:23:47.843: E/AndroidRuntime(2207): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-02 05:23:47.843: E/AndroidRuntime(2207): at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-02 05:23:47.843: E/AndroidRuntime(2207): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
我也在关注本教程的 ListView 示例 http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/