我创建的任务的项目,我是新来的机器人,而我想从很常见的URL访问JSON http://api.androidhive.info/contacts/
,
问题:我试图读取URL并获取和解析这个URL返回的JSON,
我已经添加下面一行到我的AndroidManifest.xml
<使用-权限的Android:名称=android.permission.INTERNET对/>
preferences:和我的Android preferences是
<使用-SDK
安卓的minSdkVersion =8
机器人:targetSdkVersion =18/>
在API级别18 的Android 4.3
这是怎么我试图读取URL
静态的InputStream是= NULL;
DefaultHttpClient的HttpClient =新DefaultHttpClient();
HttpPost httpPost =新HttpPost(URL);
HTT presponse HTT presponse = httpClient.execute(httpPost);
HttpEntity httpEntity = HTT presponse.getEntity();
是= httpEntity.getContent();
错误信息
五月11号至二号:23:47.843:E / AndroidRuntime(2207):致命异常:主要
五月11日至2日:23:47.843:E / AndroidRuntime(2207):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.me.countrypedia / com.me.countrypedia.MainActivity}:android.os.NetworkOnMainThreadException
五月11日至2日:23:47.843:E / AndroidRuntime(2207):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
五月11日至2日:23:47.843:E / AndroidRuntime(2207):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
五月11日至2日:23:47.843:E / AndroidRuntime(2207):在android.app.ActivityThread.access $ 600(ActivityThread.java:141)
五月11日至2日:23:47.843:E / AndroidRuntime(2207):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1256)
另外我下面这个教程的ListView例 http://www.androidhive.info/2011/11/android- XML解析 - 教程/
解决方案您异常
实际上告诉你到底你在做什么错。你是不是用另一个线程执行 NetworkOperations
您code连接到URL应该是 AsyncTasks doInBackground()
方法,关闭用户界面内被执行-Thread。
看看如何使用AsyncTask的这个问题: 如何使用AsyncTask的
I'm creating an project for assignment, I'm new to android, And I wanted to access json from very common url http://api.androidhive.info/contacts/
,
Problem: I'm trying to read the url and fetch and parse the json returned by this url,
I've already added following line into my AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
Preferences: and my android preferences are
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
Api level 18 Android 4.3
and this is how I'm trying to read the url
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();
The Error Message
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)
Also I'm following this tutorial for ListView Example http://www.androidhive.info/2011/11/android-xml-parsing-tutorial/
解决方案Your Exception
actually tells you exactly what you are doing wrong. You are not using another thread to perform NetworkOperations.
Your code that connects to the url should be executed inside an AsyncTasks doInBackground()
method, off the UI-Thread.
Take a look at this question on how to use the AsyncTask: How to use AsyncTask