解析错误数据人​​品0 org.json.jsonexception输入端 [英] Error parsing data org.json.jsonexception end of input at character 0

查看:302
本文介绍了解析错误数据人​​品0 org.json.jsonexception输入端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我整天一直在寻找在我的code和找不到为什么会发生(或为什么它实际上不返回结果形成它应该在MySQL服务器。
希望一双崭新的眼睛​​会发现原因。

该Jsonparser类

 公共类JSONParser {    静态InputStream为= NULL;
    静态的JSONObject jObj = NULL;
    静态JSON字符串=;    //构造
    公共JSONParser(){    }    //函数得到JSON的网址
    //通过HTTP POST或GET方法
    公众的JSONObject makeHtt prequest(URL字符串,字符串的方法,
            清单<&的NameValuePair GT; PARAMS){        //使HTTP请求
        尝试{            //检查请求的方法
            如果(方法==POST){
                //请求方法是POST
                // defaultHttpClient
                DefaultHttpClient的HttpClient =新DefaultHttpClient();
                HttpPost httpPost =新HttpPost(URL);
                httpPost.setEntity(新UrlEn codedFormEntity(PARAMS));                HTT presponse HTT presponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = HTT presponse.getEntity();
                是= httpEntity.getContent();            }否则如果(方法==GET){
                //请求方法是GET
                DefaultHttpClient的HttpClient =新DefaultHttpClient();
                字符串中的paramString = URLEn codedUtils.format(参数,可以UTF-8);
                网址+ =? +中的paramString;
                HTTPGET HTTPGET =新HTTPGET(URL);                HTT presponse HTT presponse = httpClient.execute(HTTPGET);
                HttpEntity httpEntity = HTT presponse.getEntity();
                是= httpEntity.getContent();
            }
        }赶上(UnsupportedEncodingException五){
            e.printStackTrace();
        }赶上(ClientProtocolException E){
            e.printStackTrace();
        }赶上(IOException异常五){
            e.printStackTrace();
        }        尝试{
            读者的BufferedReader =新的BufferedReader(新的InputStreamReader(
                    是,ISO-8859-1),8);
            StringBuilder的SB =新的StringBuilder();
            串线= NULL;
            而((行= reader.readLine())!= NULL){
                sb.append(行+\\ n);
            }
            is.close();
            JSON = sb.toString();
        }赶上(例外五){
            Log.e(缓冲区错误,错误转换结果+ e.toString());
        }        //尝试分析字符串到一个JSON对象
        尝试{
            jObj =新的JSONObject(JSON);
        }赶上(JSONException E){
            Log.e(JSON解析器,错误分析数据+ e.toString());
        }        //返回JSON字符串
        返回jObj;    }
}

我的活动范围内的内部类

 类GetUserDetails扩展的AsyncTask<字符串,字符串,字符串> {        / **
         *启动后台线程显示进度对话框之前
         * * /
        @覆盖
        在preExecute保护无效(){
            super.on preExecute();
            pDialog =新ProgressDialog(LogIn.this);
            pDialog.setMessage(加载用户的详细信息,请稍候...);
            pDialog.setIndeterminate(假);
            pDialog.setCancelable(真);
            pDialog.show();
        }        / **
         *获取产品的详细信息在后台线程
         * * /
        保护字符串doInBackground(字符串... PARAMS){                        //大厦参数
                        清单<&的NameValuePair GT;参数=新的ArrayList<&的NameValuePair GT;();
                        param.add(新BasicNameValuePair(电子邮件,EMAIL));
                        param.add(新BasicNameValuePair(密码,PASSWORD));                        //通过HTTP请求获得产品的详细信息
                        //注意产品的详细信息URL会使用GET请求
                        JSONObject的jsonuser = jsonParser.makeHtt prequest(
                                url_user_detials,GET,参数);                        //检查你的日志,JSON响应
                        Log.d(用户详细信息,jsonuser.toString());                        // JSON的成功标签
                    尝试{
                        INT成功= jsonuser.getInt(TAG_SUCCESS);
                        如果(成功== 1){
                            //成功接收产品的详细信息
                            JSONArray productObj = jsonuser
                                    .getJSONArray(产品); // JSON数组                            //从JSON数组的第一个产品对象
                            的JSONObject产物= productObj.getJSONObject(0);                            共享preferences设置= getShared preferences(preFS_NAME,0);
                            共享preferences.Editor编辑= settings.edit();                            editor.putInt(USERID(整数)product.get(概要文件));
                            editor.commit();
                            意向意图=新意图(LogIn.this,MyLists.class);
                            LogIn.this.startActivity(意向);
                            完();
                        }其他{
                            //产品具有pid未找到
                        }
                    }赶上(JSONException E){
                        e.printStackTrace();
                    }            返回null;
        }
        / **
         *在完成后台任务之后辞退进度对话框
         * ** /
        保护无效onPostExecute(字符串FILE_URL){
            //关闭该对话框一旦得了所有细节
            pDialog.dismiss();
        }
    }

和最后在服务器上的PHP脚本与数据库聊天

 < PHP/ *
 *继code将得到单品细节
 *一个产品是由产品id标识(PID)
 * ///数组JSON响应
$响应=阵列();
//其中包括DB连接类
require_once __DIR__。 /db_connect.php';//连接到数据库
$ DB =新DB_CONNECT();//检查数据后
如果(使用isset($ _ GET [电子邮件]&放大器;&安培;使用isset($ _ GET ['密码')){
    $电子邮件= $ _GET [电子邮件];
    $密码= $ _GET ['密码'];    //从产品表中的产品
    $结果= mysql_query(SELECT * FROM profiles_profiles WHERE ProfileEmail ='$电子邮件'AND ProfilePassword ='​​$密码');    如果(!空($结果)){
        //检查空结果
        如果(mysql_num_rows($结果)大于0){            $结果= mysql_fetch_array($结果);            $产品=阵列();
            $产品[概要文件] = $结果[提供ProfileID];
            //成功
            $响应[成功] = 1;            //用户节点
            $响应[产品] =阵列();            array_push($响应[产品],$产品);            //呼应JSON响应
            回声json_en code($响应);
        }其他{
            //没有产品找到
            $响应[成功] = 0;
            $响应[消息] =找不到用户;            //回声没有用户JSON
            回声json_en code($响应);
        }
    }其他{
        //没有产品找到
        $响应[成功] = 0;
        $响应[消息] =找不到用户;        //回声没有用户JSON
        回声json_en code($响应);
    }
}其他{
    //必填字段缺失
    $响应[成功] = 0;
    $响应[消息] =必填字段(s)是失踪;    //呼应JSON响应
    回声json_en code($响应);
}
?>

和实际错误的堆栈跟踪我正在

  08-18 18:43:55.573:E / JSON解析器(16008):在字符输入0结束:错误解析数据org.json.JSONException
08-18 18:43:55.573:W / dalvikvm(16008):主题ID = 13:螺纹未捕获的异常(组= 0x41515450)退出
08-18 18:43:55.584:E / AndroidRuntime(16008):致命异常:AsyncTask的#1
08-18 18:43:55.584:E / AndroidRuntime(16008):了java.lang.RuntimeException:执行doInBackground发生错误()
08-18 18:43:55.584:E / AndroidRuntime(16008):在android.os.AsyncTask $ 3.done(AsyncTask.java:299)
08-18 18:43:55.584:E / AndroidRuntime(16008):在java.util.concurrent.FutureTask中$ Sync.innerSetException(FutureTask.java:273)
08-18 18:43:55.584:E / AndroidRuntime(16008):在java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-18 18:43:55.584:E / AndroidRuntime(16008):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:307)
08-18 18:43:55.584:E / AndroidRuntime(16008):在java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-18 18:43:55.584:E / AndroidRuntime(16008):在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:230)
08-18 18:43:55.584:E / AndroidRuntime(16008):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-18 18:43:55.584:E / AndroidRuntime(16008):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:569)
08-18 18:43:55.584:E / AndroidRuntime(16008):在java.lang.Thread.run(Thread.java:856)
08-18 18:43:55.584:E / AndroidRuntime(16008):致:显示java.lang.NullPointerException
08-18 18:43:55.584:E / AndroidRuntime(16008):在com.test.app.LogIn $ GetUserDetails.doInBackground(LogIn.java:149)
08-18 18:43:55.584:E / AndroidRuntime(16008):在com.test.app.LogIn $ GetUserDetails.doInBackground(LogIn.java:1)
08-18 18:43:55.584:E / AndroidRuntime(16008):在android.os.AsyncTask $ 2.call(AsyncTask.java:287)
08-18 18:43:55.584:E / AndroidRuntime(16008):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305)
08-18 18:43:55.584:E / AndroidRuntime(16008):... 5个

和从PHP错误

  PHP解析错误:语法错误,意想不到的T_BOOLEAN_AND,在/home/sites/pmclient.co.uk/public_html/dadgapp/get_user_details.php在期待','或')'第19行


解决方案

您忘了;在:结果

  $密码= $ _GET ['密码']

在你的PHP code结果一致。

I have been looking over my code all day and can not find why this is happening (or why its not actually returning the result form the MySQL server it should. Hopefully a fresh pair of eyes will find the reason.

The Jsonparser class

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();
            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);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}

The inner class within my activity

class GetUserDetails extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(LogIn.this);
            pDialog.setMessage("Loading User details. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        /**
         * Getting product details in background thread
         * */
        protected String doInBackground(String... params) {

                        // Building Parameters
                        List<NameValuePair> param = new ArrayList<NameValuePair>();
                        param.add(new BasicNameValuePair("email", EMAIL));
                        param.add(new BasicNameValuePair("password", PASSWORD));

                        // getting product details by making HTTP request
                        // Note that product details url will use GET request
                        JSONObject jsonuser = jsonParser.makeHttpRequest(
                                url_user_detials, "GET", param);

                        // check your log for json response
                        Log.d("User Details", jsonuser.toString());

                        // json success tag
                    try {
                        int success = jsonuser.getInt(TAG_SUCCESS);
                        if (success == 1) {
                            // successfully received product details
                            JSONArray productObj = jsonuser
                                    .getJSONArray("product"); // JSON Array

                            // get first product object from JSON Array
                            JSONObject product = productObj.getJSONObject(0);

                            SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
                            SharedPreferences.Editor editor = settings.edit();

                            editor.putInt("USERID", (Integer) product.get("ProfileID")); 
                            editor.commit();
                            Intent intent = new Intent(LogIn.this, MyLists.class);
                            LogIn.this.startActivity(intent);
                            finish();
                        }else{
                            // product with pid not found
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

            return null;
        }


        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once got all details
            pDialog.dismiss();
        }
    }

And finally the PHP script on the server talking with the database

    <?php

/*
 * Following code will get single product details
 * A product is identified by product id (pid)
 */

// array for JSON response
$response = array();


// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// check for post data
if (isset($_GET['email'] && isset($_GET['password')) {
    $email = $_GET['email'];
    $password = $_GET['password'];

    // get a product from products table
    $result = mysql_query("SELECT * FROM profiles_profiles WHERE ProfileEmail = '$email' AND ProfilePassword = '$password'");

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

            $result = mysql_fetch_array($result);

            $product = array();
            $product["ProfileID"] = $result["ProfileID"];
            // success
            $response["success"] = 1;

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

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

            // echoing JSON response
            echo json_encode($response);
        } else {
            // no product found
            $response["success"] = 0;
            $response["message"] = "No user found";

            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "No user found";

        // 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);
}
?>

and the stack trace for the actual error that I am getting

08-18 18:43:55.573: E/JSON Parser(16008): Error parsing data org.json.JSONException: End of input at character 0 of 
08-18 18:43:55.573: W/dalvikvm(16008): threadid=13: thread exiting with uncaught exception (group=0x41515450)
08-18 18:43:55.584: E/AndroidRuntime(16008): FATAL EXCEPTION: AsyncTask #1
08-18 18:43:55.584: E/AndroidRuntime(16008): java.lang.RuntimeException: An error occured while executing doInBackground()
08-18 18:43:55.584: E/AndroidRuntime(16008):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
08-18 18:43:55.584: E/AndroidRuntime(16008):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-18 18:43:55.584: E/AndroidRuntime(16008):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-18 18:43:55.584: E/AndroidRuntime(16008):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-18 18:43:55.584: E/AndroidRuntime(16008):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-18 18:43:55.584: E/AndroidRuntime(16008):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-18 18:43:55.584: E/AndroidRuntime(16008):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-18 18:43:55.584: E/AndroidRuntime(16008):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-18 18:43:55.584: E/AndroidRuntime(16008):    at java.lang.Thread.run(Thread.java:856)
08-18 18:43:55.584: E/AndroidRuntime(16008): Caused by: java.lang.NullPointerException
08-18 18:43:55.584: E/AndroidRuntime(16008):    at com.test.app.LogIn$GetUserDetails.doInBackground(LogIn.java:149)
08-18 18:43:55.584: E/AndroidRuntime(16008):    at com.test.app.LogIn$GetUserDetails.doInBackground(LogIn.java:1)
08-18 18:43:55.584: E/AndroidRuntime(16008):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-18 18:43:55.584: E/AndroidRuntime(16008):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-18 18:43:55.584: E/AndroidRuntime(16008):    ... 5 more

and error from PHP

PHP Parse error:  syntax error, unexpected T_BOOLEAN_AND, expecting ',' or ')' in /home/sites/pmclient.co.uk/public_html/dadgapp/get_user_details.php on line 19

解决方案

you forgot ";" in:

$password = $_GET['password']


line in your PHP code.

这篇关于解析错误数据人​​品0 org.json.jsonexception输入端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆