我很抱歉这个菜鸟问题,但我只是不知道如何解决这个问题......
1:我在连接 USB 的手机上测试时收到此错误,但在执行搜索时在 AVD 上没有收到此错误。
Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
2:在 AVD 中运行时,第一次 JSON 搜索成功,但一旦退出并尝试访问新搜索,结果为空。下面是搜索后实例化的 JSON 列表活动:
package com.example.georgiahistoricalmarkers;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MainListActivity extends ListActivity {
// url to make request
private static String url = "http://www.georgiaplanning.com/DCAMarkerService/dcamarkerservice.markerservice.svc/rest/Locations?SearchText=";
// JSON Node names
private static final String TAG_MARKERSINRANGE = "GetAllMarkersInRangeResult";
private static final String TAG_DCAID = "DCA_ID";
private static final String TAG_MARKERTITLE = "MarkerTitle";
private static final String TAG_MARKERLATITUDE = "MarkerLatitude";
private static final String TAG_MARKERLONGITUDE = "MarkerLongitude";
// contacts JSONArray
JSONArray markersInRange = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String value = null;
Bundle extras = getIntent().getExtras();
if(extras != null){
value = extras.getString("markerSearch");
}
url = (url+value);
// Hashmap for ListView
ArrayList<HashMap<String, String>> markerList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
markersInRange = json.getJSONArray(TAG_MARKERSINRANGE);
// looping through All Contacts
for(int i = 0; i < markersInRange.length(); i++){
JSONObject c = markersInRange.getJSONObject(i);
// Storing each json item in variable
int dcaid = c.getInt(TAG_DCAID);
String markertitle = c.getString(TAG_MARKERTITLE);
double markerlongitude = c.getDouble(TAG_MARKERLATITUDE);
double markerlatitude = c.getDouble(TAG_MARKERLONGITUDE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_DCAID, Integer.toString(dcaid));
map.put(TAG_MARKERTITLE, markertitle);
map.put(TAG_MARKERLONGITUDE, Double.toString(markerlongitude));
map.put(TAG_MARKERLONGITUDE, Double.toString(markerlatitude));
// adding HashList to ArrayList
markerList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, markerList,
R.layout.list_item,
new String[] { TAG_MARKERTITLE, TAG_DCAID }, new int[] {
R.id.title, R.id.dcaid });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String passId = ((TextView) view.findViewById(R.id.dcaid)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleItemActivity.class);
in.putExtra(TAG_DCAID, passId);
startActivity(in);
}
});
}
}
如果这可能有助于理解这个问题,下面是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.georgiahistoricalmarkers"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.georgiahistoricalmarkers.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainListActivity"
android:label="MainListActivity" >
</activity>
<activity
android:name=".SingleItemActivity"
android:label="SingleItemActivity" >
</activity>
</application>