我正在尝试解析 JSON,我正在使用这个示例。
在类解析器的示例中,没有使用辅助线程,我得到NetworkOnMainThreadException。
为了解决这个问题,我在 Asynctask 中引入了代码,但由于在线出现错误而无法正确传输数据:
contacts = json.getJSONArray(TAG_CONTACTS);
这是我正在使用的简单代码
JSONParser.java
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
new ParseJson().execute(url);
return jObj;
}
private class ParseJson extends AsyncTask<String, Void, JSONObject>{
protected JSONObject doInBackground(String... params) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(params[0]);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
}
MainActivity.java
public class MainActivity extends Activity{
// url to make request
private static String url = "http://api.androidhive.info/contacts/";
// JSON Node names
private static final String TAG_CONTACTS = "contacts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
// contacts JSONArray
JSONArray contacts = null;
JSONObject json;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new task().execute();
}
public void loadAreas(){
try {
// looping through All Contacts
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
Log.e("TAG","ID: "+id+" - "+"NAME: "+name);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
private class task extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try {
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
// Getting Array of Contacts
contacts = json.getJSONArray(TAG_CONTACTS);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
loadAreas();
}
}
}
我的JSON数据
{
"contacts": [
{
"id": "c200",
"name": "Ravi Tamada",
"email": "ravi@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c201",
"name": "Johnny Depp",
"email": "johnny_depp@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c202",
"name": "Leonardo Dicaprio",
"email": "leonardo_dicaprio@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c203",
"name": "John Wayne",
"email": "john_wayne@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c204",
"name": "Angelina Jolie",
"email": "angelina_jolie@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c205",
"name": "Dido",
"email": "dido@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c206",
"name": "Adele",
"email": "adele@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c207",
"name": "Hugh Jackman",
"email": "hugh_jackman@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c208",
"name": "Will Smith",
"email": "will_smith@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c209",
"name": "Clint Eastwood",
"email": "clint_eastwood@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c2010",
"name": "Barack Obama",
"email": "barack_obama@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c2011",
"name": "Kate Winslet",
"email": "kate_winslet@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "female",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
},
{
"id": "c2012",
"name": "Eminem",
"email": "eminem@gmail.com",
"address": "xx-xx-xxxx,x - street, x - country",
"gender" : "male",
"phone": {
"mobile": "+91 0000000000",
"home": "00 000000",
"office": "00 000000"
}
}
]
}
日志猫
04-01 16:06:48.414: W/dalvikvm(9151): threadid=11: thread exiting with uncaught exception (group=0x40d09930)
04-01 16:06:48.422: E/AndroidRuntime(9151): FATAL EXCEPTION: AsyncTask #1
04-01 16:06:48.422: E/AndroidRuntime(9151): java.lang.RuntimeException: An error occured while executing doInBackground()
04-01 16:06:48.422: E/AndroidRuntime(9151): at android.os.AsyncTask$3.done(AsyncTask.java:299)
04-01 16:06:48.422: E/AndroidRuntime(9151): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
04-01 16:06:48.422: E/AndroidRuntime(9151): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
04-01 16:06:48.422: E/AndroidRuntime(9151): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
04-01 16:06:48.422: E/AndroidRuntime(9151): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-01 16:06:48.422: E/AndroidRuntime(9151): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
04-01 16:06:48.422: E/AndroidRuntime(9151): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
04-01 16:06:48.422: E/AndroidRuntime(9151): at java.lang.Thread.run(Thread.java:856)
04-01 16:06:48.422: E/AndroidRuntime(9151): Caused by: java.lang.NullPointerException
04-01 16:06:48.422: E/AndroidRuntime(9151): at com.example.json_parser.MainActivity$task.doInBackground(MainActivity.java:63)
04-01 16:06:48.422: E/AndroidRuntime(9151): at com.example.json_parser.MainActivity$task.doInBackground(MainActivity.java:1)
04-01 16:06:48.422: E/AndroidRuntime(9151): at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-01 16:06:48.422: E/AndroidRuntime(9151): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
04-01 16:06:48.422: E/AndroidRuntime(9151): ... 4 more
04-01 16:06:48.461: D/libEGL(9151): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
04-01 16:06:48.476: D/libEGL(9151): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
04-01 16:06:48.484: D/libEGL(9151): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
04-01 16:06:48.570: D/OpenGLRenderer(9151): Enabling debug mode 0
04-01 16:06:50.273: D/dalvikvm(9151): GC_CONCURRENT freed 182K, 3% free 8885K/9100K, paused 3ms+23ms, total 41ms
我有那个问题?
我错误地返回 Json 对象?
谢谢!