简体   繁体   中英

Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

Ejects the following error when the application is run, I have two classes one ListViewAdapter other is JSONFunctions? What is a mistake, maybe it's my URL? my URL to the API 77.105.36.203/api/v1/info or problem is function 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();
    }
}

This is error log.

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

Check this code in your doInBackground() method, and tell me if works.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM