解析数据时出错org.json.JSONException:字符输入结束

时间:2013-03-10 14:03:43

标签: php android json parsing

当我尝试从我的数据库中获取数据(带有ë,é,ä等文本......)到我的Android应用程序时,我收到此错误,我尝试了BufferedReader reader = new BufferedReader(new InputStreamReader(是,“UTF) -8“),8)和BufferedReader reader = new BufferedReader(new InputStreamReader(is,”iso-8859-1“),8)但我仍然得到相同的错误!

有人可以告诉我我做错了什么或错过了吗?

这是我的JsonParser

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 = "";

    // constructor
    public JSONParser() {

    }

    // function get json from url
    // by making HTTP POST or GET method
    public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        // Making HTTP request
        try {

            // check for request method
            if (method == "POST") {
                // request method is 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") {
                // request method is 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();
            Log.e("Buffer Error",
                    "getting JSon String from server :-- " + sb.toString());
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
            System.out.println(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}

这是我的PHP代码:

<?php header('content-type: application/json; charset=utf-8');


$response = array();


require_once __DIR__ . '/db_connect.php';


$db = new DB_CONNECT();



// check for post data
if (isset($_GET["id"])) {
    $pid1 = $_GET['id'];


// get a Recette from Recettes table
$result = mysql_query("SELECT id, naam, foto, ingredienten, bereiding, personen, bereidingstijd, serveertips FROM Recette WHERE id = $pid1");

if (!empty($result)) {
    // check for empty result
    if (mysql_num_rows($result) > 0) {

        $result = mysql_fetch_array($result);

        $Recette = array();
        $Recette["id"] = $result["id"];
        $Recette["naam"] = $result["naam"];
        $Recette["foto"] = $result["foto"];
        $Recette["ingredienten"] = $result["ingredienten"];
        $Recette["bereiding"] = $result["bereiding"];
        $Recette["personen"] = $result["personen"];
        $Recette["bereidingstijd"] = $result["bereidingstijd"];
        $Recette["serveertips"] = $result["serveertips"];           
        // success
        $response["success"] = 1;

        // user node
        $response["Recette"] = array();

        array_push($response["Recette"], $Recette);

        // echoing JSON response
        echo json_encode($response);
    } else {
        // no Recette found
        $response["success"] = 0;
        $response["message"] = "Geen Recette gevonden";

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // no Recette found
    $response["success"] = 0;
    $response["message"] = "Geen Recette gevonden";

    // echo no users JSON
    echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

这是错误日志:

03-10 16:21:11.740: E/Buffer Error(25399): getting JSon String from server :-- 
03-10 16:21:11.740: E/JSON Parser(25399): Error parsing data org.json.JSONException: End of input at character 2 of 
03-10 16:21:11.745: D/Recette(25399): {"success":1,"Recette":[{"id":"51","naam":"Gegratineerde aubergine met tonijn","foto":"http:\/\/www.betiben1.0fees.net\/image\/tonijnaub.png"},{"id":"49","naam":"Paprika's met arganolie","foto":"http:\/\/www.betiben1.0fees.net\/image\/paprika_arganolie.png"},{"id":"48","naam":"Warme Dadelmelk","foto":"http:\/\/www.betiben1.0fees.net\/image\/dadelmelk.jpg"},{"id":"47","naam":"Amandel-dadelmelk","foto":"http:\/\/www.betiben1.0fees.net\/image\/dadelmelk1.png"},{"id":"46","naam":"Muntthee (Attay benaanaa)","foto":"http:\/\/www.betiben1.0fees.net\/image\/thee.png"},{"id":"45","naam":"Zaalouk (auberginepuree)","foto":"http:\/\/www.betiben1.0fees.net\/image\/tajin.png"},{"id":"44","naam":"Zaalouk (auberginepuree)","foto":"http:\/\/www.betiben1.0fees.net\/image\/zaalouk.png"},{"id":"37","naam":"Dadelruiten","foto":"http:\/\/www.betiben1.0fees.net\/image\/dadelruiten.png"},{"id":"36","naam":"maakoada","foto":"http:\/\/www.betiben1.0fees.net\/image\/maakoda.png"},{"id":"35","naam":"Groentetajine met meiknol en specerijen\n","foto":"http:\/\/www.betiben1.0fees.net\/image\/Groentetajine_meiknol.png"}]}
03-10 16:21:11.995: D/dalvikvm(25399): GC_CONCURRENT freed 623K, 11% free 16627K/18503K, paused 13ms+3ms, total 47ms
03-10 16:21:11.995: D/dalvikvm(25399): WAIT_FOR_CONCURRENT_GC blocked 27ms
03-10 16:21:14.415: W/System.err(25399): org.json.JSONException: No value for ingredienten
03-10 16:21:14.430: W/System.err(25399):    at org.json.JSONObject.get(JSONObject.java:354)
03-10 16:21:14.430: W/System.err(25399):    at org.json.JSONObject.getString(JSONObject.java:510)
03-10 16:21:14.430: W/System.err(25399):    at com.youness.tajineenzo.ReceptOverzicht.onCreate(ReceptOverzicht.java:159)
03-10 16:21:14.430: W/System.err(25399):    at android.app.Activity.performCreate(Activity.java:5206)
03-10 16:21:14.435: W/System.err(25399):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
03-10 16:21:14.440: W/System.err(25399):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
03-10 16:21:14.440: W/System.err(25399):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
03-10 16:21:14.440: W/System.err(25399):    at android.app.ActivityThread.access$700(ActivityThread.java:140)
03-10 16:21:14.445: W/System.err(25399):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
03-10 16:21:14.445: W/System.err(25399):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-10 16:21:14.445: W/System.err(25399):    at android.os.Looper.loop(Looper.java:137)
03-10 16:21:14.450: W/System.err(25399):    at android.app.ActivityThread.main(ActivityThread.java:4921)
03-10 16:21:14.450: W/System.err(25399):    at java.lang.reflect.Method.invokeNative(Native Method)
03-10 16:21:14.455: W/System.err(25399):    at java.lang.reflect.Method.invoke(Method.java:511)
03-10 16:21:14.455: W/System.err(25399):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
03-10 16:21:14.460: W/System.err(25399):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
03-10 16:21:14.460: W/System.err(25399):    at dalvik.system.NativeStart.main(Native Method)
03-10 16:21:14.665: W/ResourceType(25399): Failure getting entry for 0x010802c1 (t=7 e=705) in package 0 (error -75)
03-10 16:21:14.675: W/ResourceType(25399): Failure getting entry for 0x010802c1 (t=7 e=705) in package 0 (error -75)
03-10 16:21:14.680: W/ResourceType(25399): Failure getting entry for 0x010802c1 (t=7 e=705) in package 0 (error -75)

0 个答案:

没有答案