简体   繁体   中英

Error parsing dataorg.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject

i'm trying to make android food order for my thesis and because this error i'm running out of time :(

error on logcat :

Error parsing dataorg.json.JSONException: Value cannot be converted to JSONObject org.json.JSONException: Value to JSONObject

here's my JSONParser :

package com.makanan.restotradisional;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

public JSONParser() {
}

// fungsi abil json url lewat method HTTP POST atau GET
public JSONObject makeHttpRequest(String url, String method,
        List<NameValuePair> params) {
    try {
        if (method == "POST") {
            // jika request method adalah POST
            // defaultHttpClient

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        } else if (method == "GET") {
            // jika request method adalah GET

            DefaultHttpClient httpClient = new DefaultHttpClient();
            String paramString = URLEncodedUtils.format(params, "utf-8");
            url += "?" + paramString;
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            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 {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data" + e.toString());
        e.printStackTrace();
    }
    return jObj;

}
}

this my PHP & Java : http://www.4shared.com/rar/1lGplX19ba/Java_and_PHP.html

and this is my database on phpmyadmin : http://www.4shared.com/rar/y_UMtL7_ce/rumah_makan.html

please help me

Remove any of the <br> statements or echo statements from your php file except the one that you are using to pass json..

Check the output of your file in browser, remove all the unwanted things other than json..

Please print and check if your string json is in right format as expected by the JSONObject constructor. Per documentation, valid json string to construct JSONObject should be -- A string beginning with { (left brace) and ending with } (right brace).

Please refer this .

Go inside JSONParser and do this so u see in logcat whats comming from php. Probably its a php error.

try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        //This line is what u need to add
        Log.d("Whats wrong?", json.toString());
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

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