发布到php脚本时发生JSON解析错误

时间:2013-08-23 12:04:22

标签: php android database json

我正在尝试在Android中加载方法的参数,并通过http将请求通过我自己的服务器在线发送到php文件,其中它进行SQL查询并根据由该参数发送的相关元素提取Android http请求。

我使用该参数发送/发出HTTP请求的函数

jsonFUNC

// Creating JSON Parser instance
JSONparser jParser = new JSONparser();
// getting JSON string from URL
//JSONObject json = jParser.getJSONFromUrl(url_product_details);
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user_id", user_id));

// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject json = jParser.makeHttpRequest(
        url_product_details, "GET", params);

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

我完整的JSONparser类承载了makeHttpRequest()函数,该函数运行得很好我可以说至少函数getJSONFromUrl:

package library;
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();
            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;

    }
    public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {

        // Making HTTP request
        try {
            // 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();

        } 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();
            Log.e("JSON", json);
        } 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;

    }
}

我在服务器上的PHP代码文件:

<?php

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

// include db connect class
require_once '/DB_Connect.php';

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

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

    // get a product from products table
    $result = mysql_query("SELECT HTTP FROM INFO WHERE user_id = $user_id");

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

            $result = mysql_fetch_array($result);

            $user_info = array();
            $user_info["HTTP"] = $result["HTTP"];
            // success
            $response["success"] = 1;

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

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

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

            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "No product 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);
}
?>

我收到的错误:

08-23 17:20:01.780: E/JSON Parser(23489): Error parsing data org.json.JSONException: End of input at character 0 of 

(是的,我没有得到完整的错误,直到字符0) *然后VM关闭,我得到以下错误:*

08-23 17:20:01.780: E/AndroidRuntime(23489): FATAL EXCEPTION: main
08-23 17:20:01.780: E/AndroidRuntime(23489): java.lang.NullPointerException
08-23 17:20:01.780: E/AndroidRuntime(23489):    at com.database_demo.DashboardActivity.jsonFUNC(DashboardActivity.java:237)
08-23 17:20:01.780: E/AndroidRuntime(23489):    at com.database_demo.DashboardActivity$2.onClick(DashboardActivity.java:111)
08-23 17:20:01.780: E/AndroidRuntime(23489):    at android.view.View.performClick(View.java:4223)
08-23 17:20:01.780: E/AndroidRuntime(23489):    at android.view.View$PerformClick.run(View.java:17275)
08-23 17:20:01.780: E/AndroidRuntime(23489):    at android.os.Handler.handleCallback(Handler.java:615)
08-23 17:20:01.780: E/AndroidRuntime(23489):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-23 17:20:01.780: E/AndroidRuntime(23489):    at android.os.Looper.loop(Looper.java:137)
08-23 17:20:01.780: E/AndroidRuntime(23489):    at android.app.ActivityThread.main(ActivityThread.java:4898)
08-23 17:20:01.780: E/AndroidRuntime(23489):    at java.lang.reflect.Method.invokeNative(Native Method)
08-23 17:20:01.780: E/AndroidRuntime(23489):    at java.lang.reflect.Method.invoke(Method.java:511)
08-23 17:20:01.780: E/AndroidRuntime(23489):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
08-23 17:20:01.780: E/AndroidRuntime(23489):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
08-23 17:20:01.780: E/AndroidRuntime(23489):    at dalvik.system.NativeStart.main(Native Method)

强调文字

更新了使用POST的问题

08-24 19:43:23.685: E/JSON Parser(6982): Error parsing data org.json.JSONException: End of input at character 0 of 
08-24 19:43:23.685: W/System.err(6982): org.json.JSONException: No value for information
08-24 19:43:23.685: W/System.err(6982):     at org.json.JSONObject.get(JSONObject.java:354)
08-24 19:43:23.685: W/System.err(6982):     at org.json.JSONObject.getJSONArray(JSONObject.java:544)
08-24 19:43:23.690: W/System.err(6982):     at com.database_demo.DashboardActivity.jsonFUNC(DashboardActivity.java:233)
08-24 19:43:23.690: W/System.err(6982):     at com.database_demo.DashboardActivity$2.onClick(DashboardActivity.java:111)
08-24 19:43:23.690: W/System.err(6982):     at android.view.View.performClick(View.java:4223)
08-24 19:43:23.695: W/System.err(6982):     at android.view.View$PerformClick.run(View.java:17275)
08-24 19:43:23.695: W/System.err(6982):     at android.os.Handler.handleCallback(Handler.java:615)
08-24 19:43:23.695: W/System.err(6982):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-24 19:43:23.695: W/System.err(6982):     at android.os.Looper.loop(Looper.java:137)
08-24 19:43:23.695: W/System.err(6982):     at android.app.ActivityThread.main(ActivityThread.java:4898)
08-24 19:43:23.695: W/System.err(6982):     at java.lang.reflect.Method.invokeNative(Native Method)
08-24 19:43:23.695: W/System.err(6982):     at java.lang.reflect.Method.invoke(Method.java:511)
08-24 19:43:23.695: W/System.err(6982):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
08-24 19:43:23.700: W/System.err(6982):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
08-24 19:43:23.700: W/System.err(6982):     at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案