【问题标题】:Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObjectjava.lang.String 类型的值 <!DOCTYPE 无法转换为 JSONObject
【发布时间】:2014-07-10 22:51:42
【问题描述】:

应用程序运行时弹出以下错误,我有两个类,一个是 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

【问题讨论】:

    标签: java android json image


    【解决方案1】:

    在您的 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();
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-02-03
      • 2017-10-02
      • 2013-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多