检索JSON对象的Android [英] Retrieve JSON Object Android
本文介绍了检索JSON对象的Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图从URL获得一个JSON字符串,但我不断收到一个NetworkOnMainThreadException。我以为我把我的网络code到另一个线程,但显然不是。
I'm trying to get a JSON String from a URL but I keep getting a "NetworkOnMainThreadException". I thought I moved my network code to another thread but obviously not.
下面是错误:
Here is the error:
JSONParser
JSONParser
package com.example.jsonparsing;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public String getJSONFromUrl(String url) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://api.androidhive.info/contacts/");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
HttpEntity entity = response.getEntity();
try {
inputStream = entity.getContent();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// json is UTF-8 by default i beleive
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result = sb.toString();
}
}
主要活动
package com.example.jsonparsing;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MainActivity extends Activity{
// url to make request
private static String url = "http://api.androidhive.info/contacts/";
// contacts JSONArray
JSONArray contacts = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
RetrieveJSONString retrieveJSON = new RetrieveJSONString();
retrieveJSON.run();
String whatever = retrieveJSON.getJSONString();
}
}
class RetrieveJSONString implements Runnable{
JSONParser jParser;
String jsonString;
public void run() {
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
String jsonString = jParser.getJSONFromUrl("http://api.androidhive.info/contacts/");
}
public String getJSONString(){
return jsonString;
}
}
清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jsonparsing"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
如果任何人都可以阐明这个问题一些轻我会AP preciate呢!
If anybody can shed some light on the problem I would appreciate it!
推荐答案
您获得的是错误,因为你正在尝试做的UI线程上的HTTP调用,你永远不应该这样做:)使用一个替代的AsyncTask。下面是一个简单的例子:
You are getting that error because you are trying to do HTTP calls on the UI-thread, and you should never do that :) Use an AsyncTask instead. Here is a simple example:
GetJsonAsync getJson = new GetJsonAsync();
getJson.execute();
private class GetJsonAsync extends AsyncTask <String, Void, String> {
@Override
protected void onPreExecute() {
// Do stuff before the operation
}
@Override
protected String doInBackground(String... params){
getJSONFromUrl();
return null;
}
@Override
protected void onPostExecute(String result) {
// Do stuff after the operation
}
}
这篇关于检索JSON对象的Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文