[英]Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
在運行應用程序時彈出以下錯誤,我有兩個類,一個是ListViewAdapter,另一個是JSONFunctions? 有什么錯誤,也許是我的網址? 我指向API 77.105.36.203/api/v1/info的URL或問題是函數doInBackgoround JSONObject嗎?
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String ID = "id";
static String IME = "ime";
static String ADRESA = "adresa";
static String SLIKA = "slika";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.listview_main, container, false);
new DownloadJSON().execute();
return rootView;
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(getActivity());
// Set progressdialog title
mProgressDialog.setTitle("Android JSON Parse Tutorial");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("http://77.105.36.203/api/v1/info");
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("objects");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("id", jsonobject.getString("id"));
map.put("ime", jsonobject.getString("ime"));
map.put("adresa", jsonobject.getString("adresa"));
map.put("slika", jsonobject.getString("slika"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) getView().findViewById(R.layout.listview_item);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(getActivity(), arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
這是錯誤日志。
07-10 16:15:59.605: E/log_tag(5389): Error parsing data [Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject]07-10 16:15:59.605: E/AndroidRuntime(5389): FATAL EXCEPTION: AsyncTask #2
在doInBackground()方法中檢查此代碼,並告訴我是否可行。
try {
// Locate the array name in JSON
//jsonarray = jsonobject.getJSONArray("objects");
JSONObject jsonObject = jsonobject.getJSONObject("objects");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
//jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("id", jsonObject.getString("id"));
map.put("ime", jsonObject.getString("ime"));
map.put("adresa", jsonObject.getString("adresa"));
map.put("slika", jsonObject.getString("slika"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.