但是我不断收到这些异常。我还在下面发布了我的 logcat。我是android新手,所以我需要帮助。url 由一组 json 对象组成。
[
{
"codeField":"COMPSCI 101",
"semesterField":"Summer School; Semester 1; Semester 2",
"titleField":"Principles of Programming"
},
{
"codeField":"COMPSCI 105",
"semesterField":"Summer School; Semester 1; Semester 2",
"titleField":"Principles of Computer Science"
},
......
]
代码 :
package com.example.compsci_734t;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
public class UoaCompsciActivity extends Activity {
private static String url = "http://redsox.tcs.auckland.ac.nz/734A/CSService.svc/courses";
//URL requestUrl = new URL(url);
JSONArray courses = new JSONArray();
private static final String TAG_COURSES = "Courses";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_uoa_compsci);
new MyTask().execute();
}
private class MyTask extends AsyncTask<URL, Void, JSONObject> {
@Override
protected JSONObject doInBackground(URL... urls) {
loadJSON(url);
return null;
}
protected void onPostExecute(JSONObject json) {
try {
courses = json.getJSONArray(TAG_COURSES);
// looping through all courses
for (int i = 0; i < courses.length(); i++) {
JSONObject c = courses.getJSONObject(i);
// Storing each json item in variable
String course_id = c.getString("courseField:");
String course_name = c.getString("titleField:");
String course_semester = c.getString("semesterField:");
Log.v("--", "Course: \n" + " " + course_id + " " + course_name
+ " " + course_semester);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
public JSONObject loadJSON(String url) {
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
}
这是我更新的代码: 现在在 logcat 中只有一个异常:05-13 23:02:04.309: E/JSON Parser(1213): Error parsing data org.json.JSONException: Value 
package com.example.compsci_734t;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
public class UoaCompsciActivity extends Activity {
private static String url = "http://redsox.tcs.auckland.ac.nz/734A/CSService.svc/courses";
//URL requestUrl = new URL(url);
JSONArray courses = null;
//private static final String TAG_COURSES = "Courses";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_uoa_compsci);
new MyTask().execute();
}
private class MyTask extends AsyncTask<URL, Void, JSONObject> {
@Override
protected JSONObject doInBackground(URL... urls) {
return loadJSON(url);
}
protected void onPostExecute(JSONObject json) {
String var = "Courses";
//JSONArray courses = new JSONArray(var);
try {
JSONArray courses = new JSONArray(var);
// looping through all courses
for (int i = 0; i < courses.length(); i++) {
JSONObject c = courses.getJSONObject(i);
// Storing each json item in variable
String course_id = c.getString("codeField:");
String course_name = c.getString("titleField:");
String course_semester = c.getString("semesterField:");
Log.v("--", "Course: \n" + " " + course_id + " " + course_name
+ " " + course_semester);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
public JSONObject loadJSON(String url) {
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
}
这是我的 jsonparser 代码
package com.example.compsci_734t;
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.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 JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
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;
}
}