Php BufferedReader中的JSONParser类出错

Php BufferedReader中的JSONParser类出错,php,android,mysql,json,Php,Android,Mysql,Json,我正在尝试将我的电话联系人保存到在线Mysql数据库。一切正常,但它在JSONParser类中返回空json对象。我调试了应用程序,发现BufferedReader中可能存在一些问题。它引发异常,因为php代码无法通过JSON创建行 public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; Stri

我正在尝试将我的电话联系人保存到在线Mysql数据库。一切正常,但它在JSONParser类中返回空json对象。我调试了应用程序,发现BufferedReader中可能存在一些问题。它引发异常,因为php代码无法通过JSON创建行

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";
    StringBuilder sb;

    // constructor
    public JSONParser() {

    }

    // function get json from url
    // by making HTTP POST or GET mehtod
    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");
                Log.d("Hope","Hope 1");
                url += "?" + paramString;
                Log.d("Hope","Hope 2");
                HttpGet httpGet = new HttpGet(url);
                Log.d("Hope","Hope 3");

                HttpResponse httpResponse = httpClient.execute(httpGet);
                Log.d("Hope","Hope 4");
                HttpEntity httpEntity = httpResponse.getEntity();
                Log.d("Hope","Hope 5");
                is = httpEntity.getContent();
            }           

        } catch (UnsupportedEncodingException e) {
            Log.d("ex1","ex1 "+e);
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            Log.d("ex1","ex2 "+e);
            e.printStackTrace();
        } catch (IOException e) {
            Log.d("ex1","ex3 "+e);
            e.printStackTrace();
        }

        try {
            //here is line which is giving some problem 
            //which I don't know and giving null JSONObject

            Log.d("Hope","Hope 6");
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            Log.d("Hope","Hope 7");
            sb = new StringBuilder();
            String line = null;

            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            Log.d("Hope","Hope 8");
            is.close();
            json = sb.toString();
            //printed json value  "json{"success":0,"message":"Oops! An error occurred"
            Log.d("eee","json"+ json);
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + json);
        }

        char[] chars = json.toCharArray();
        // try parse the string to a JSON object
        if(chars[0] != 'D'){
            try {
                jObj = new JSONObject(json);
            } catch (Exception e) {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            }
        } else {
                Log.d("null","null");
        }

    return jObj;        
    }
}
我的PHP代码是:

<?php

/*
 * Following code will create a new product row
 * All product details are read from HTTP Post Request
 */

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

// check for required fields
if (isset($_POST['name']) && isset($_POST['id']) && isset($_POST['phone'])&& isset($_POST['email'])) {

    $id = $_POST['id'];
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];

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

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

    // mysql inserting a new row
    $result = mysql_query("INSERT INTO crm(id, name, phone, email) VALUES('$id', '$name', '$phone', '$email')");

    // check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "Product successfully created.";

        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";

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

这里面有什么错误。?log cat未显示任何错误。应用程序正在正常工作,但我的json对象返回null,因此我无法将电话联系人保存到mysql数据库。调试并检查在这种情况下,如果ifchars[0]!=,是否为真D'或否。?它变为真,并且具有字符{消息:Oops!发生错误,成功:0}在此情况下,我认为它不是触发错误。请调试并检查它是否触发异常。请显示异常。否则代码很好。
<?php

/*
 * Following code will create a new product row
 * All product details are read from HTTP Post Request
 */

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

// check for required fields
if (isset($_POST['name']) && isset($_POST['id']) && isset($_POST['phone'])&& isset($_POST['email'])) {

    $id = $_POST['id'];
    $name = $_POST['name'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];

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

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

    // mysql inserting a new row
    $result = mysql_query("INSERT INTO crm(id, name, phone, email) VALUES('$id', '$name', '$phone', '$email')");

    // check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "Product successfully created.";

        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";

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