我在解析 JSON 数据时遇到了大问题。我总是收到这个错误:
error parsing data org.json.JSONException: Value <!doctype of type java.Lang.String cannot be
converted to JSONObject
我需要获取 JSON 数据并将其列在 ListView 中。我需要列出姓名、姓氏、数字和图片,因此我将非常感谢有关从 url 加载图像的一些建议,但现在主要问题是我总是收到我提到的错误。
这是代码,JSONParsing.java:
public class JSONParsing extends ListActivity {
private static String URL = "http://androidtest.apiary.io/get-users";
private static String TAG_USERS = "users";
private static String TAG_NAME = "name";
private static String TAG_LAST_NAME = "last_name";
private static String TAG_PH_NUM = "number";
JSONArray users = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String,
String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject jobj = jParser.getJSONFromUrl(URL);
try {
// Getting Array of Contacts
users = jobj.getJSONArray(TAG_USERS);
// looping through All Contacts
for(int i = 0; i < users.length(); i++){
JSONObject job = users.getJSONObject(i);
// Storing each json item in variable
String name = job.getString(TAG_NAME);
String lastName = job.getString(TAG_LAST_NAME);
String number = job.getString(TAG_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_NAME, name);
map.put(TAG_LAST_NAME, lastName);
map.put(TAG_PH_NUM, number);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.list_item,
new String[] { TAG_NAME + " " + TAG_LAST_NAME, TAG_PH_NUM }, new
int[] {
R.id.name, R.id.email, R.id.mobile });
setListAdapter(adapter);
}
}
和 JSONParser.java:
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;
}
}
还有 xml 的 list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:context=".JSONParsing" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:paddingTop="6dip"
android:textColor="#43bd00"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/lastName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="2dip"
android:textColor="#acacac" />
<TextView
android:id="@+id/number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:textColor="#acacac"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
主.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".JSONParsing" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>