Android解析第0个字符处输入的json结尾时出错


Android error parsing json end of input at character 0 of

这是整个错误消息:

    error parsing data org.json.jsonexception end of input at character 0 of

这是我的全部代码:

// JSON Node names
public static final String TAG_SUCCESS = "success";
public static final String TAG_ID = "salesorderingcard_id";
public static String TAG_CODE = "salesordercard_code";
public static final String TAG_LOCATION_TO = "location_to";
public static final String TAG_LOCATION_FROM = "location_from";
public final static String TAG_CUSTOMER = "customername";
public final static String TAG_SALESMAN = "salesmanname";

protected String doInBackground(String... args) {
            // getting updated data from EditTexts
            String salesordercard_code = etCode.getText().toString();
            String location_from = etFrom.getText().toString();
            String location_to = etTo.getText().toString();
            String customername = etCustomer.getText().toString();
            String salesmanname = etSalesman.getText().toString();
            Bundle extras = getIntent().getExtras();
            String salesorderingcard_id = extras.getString("salesorderingcard_id");;
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair(TAG_ID, salesorderingcard_id));
            params.add(new BasicNameValuePair(TAG_CODE, salesordercard_code));
            params.add(new BasicNameValuePair(TAG_LOCATION_FROM, location_from));
            params.add(new BasicNameValuePair(TAG_LOCATION_TO, location_to));
            params.add(new BasicNameValuePair(TAG_CUSTOMER, customername));
            params.add(new BasicNameValuePair(TAG_SALESMAN, salesmanname));
            Log.d("Salesman", salesmanname);
            // sending modified data through http request
            // Notice that update salesorder url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_update_salesorder,
                    "POST", params);
            // check json success tag
            try {
                int success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    // successfully updated
                    //Intent i = getIntent();
                    // send result code 100 to notify about Sales Order update
                    //setResult(100, i);
                    //finish();
                    Log.d("Update Successful", "Update Successful");
                } else {
                    // failed to update Sales Order
                    Log.d("Update failed", "Update failed");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

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.equals("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();
            Log.d("response", is.toString());
        }else if(method.equals("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();
            Log.d("response", is.toString());
        }           
    } 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;
}

update_record.php

// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['salesorderingcard_id']) && isset($_POST['salesordercard_code']) && isset($_POST['location_from']) && isset($_POST['location_to']) 
&& isset($_POST['customername']) && isset($_POST['salesmanname']) ) {
$salesorderingcard_id = $_POST['salesorderingcard_id'];
$salesordercard_code = $_POST['salesordercard_code'];
$location_from = $_POST['location_from'];
$location_to = $_POST['location_to'];
$customername = $_POST['customername'];
$salesmanname = $_POST['salesmanname'];
// include db connect class
include('connectdb.php');
// connecting to db
$db = new DB_CONNECT();
// mysql update row with matched pid
$result = mysql_query("UPDATE salesorderingcard SET salesordercard_code = '$salesordercard_code', 
location_from = '$location_from', location_to = '$location_to', customername = '$customername', salesmanname = '$salesmanname'
WHERE salesorderingcard_id = $salesorderingcard_id");
// check if row inserted or not
if ($result) {
    // successfully updated
    $response["success"] = 1;
    $response["message"] = "Sales Order successfully updated.";
    // echoing JSON response
    echo json_encode($response);
} else {
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}

我试图使用我的android测试应用程序在线更新我的记录,但我无法做到。我不知道出了什么问题,我真的需要你的帮助。非常感谢您的帮助。

更新:

我在线上得到了一个NULLPOINTERCEPTION

 int success = json.getInt(TAG_SUCCESS);

响应为:org.apache.http.conn.EOFSENSORINPUTSTREAM@417dbcf8

尝试这个

class AsyncHttpRequest extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            try {
                if (!isGetMethod) {
                    HttpPost httppost = new HttpPost(urls[0]);
                    httpclient = new DefaultHttpClient();
                    HttpConnectionParams.setConnectionTimeout(
                            httpclient.getParams(), 30000);
                    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    for (String key : map.keySet()) {
                        System.out.println(key + "  < === >  " + map.get(key));
                        nameValuePairs.add(new BasicNameValuePair(key, map
                                .get(key)));
                    }
                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    ActivityManager manager = (ActivityManager) activity
                            .getSystemService(Context.ACTIVITY_SERVICE);
                    if (manager.getMemoryClass() < 25) {
                        System.gc();
                    }
                    HttpResponse response = httpclient.execute(httppost);
                    String responseBody = EntityUtils.toString(response
                            .getEntity());
                    return responseBody;
                } else {
                    httpclient = new DefaultHttpClient();
                    HttpConnectionParams.setConnectionTimeout(
                            httpclient.getParams(), 30000);
                    HttpGet httpGet = new HttpGet(urls[0]);
                    HttpResponse httpResponse = httpclient.execute(httpGet);
                    HttpEntity httpEntity = httpResponse.getEntity();
                    String responseBody = EntityUtils.toString(httpEntity);
                    return responseBody;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } catch (OutOfMemoryError oume) {
                System.gc();
            } finally {
                if (httpclient != null)
                    httpclient.getConnectionManager().shutdown();
            }
            return null;
        }
        @Override
        protected void onPostExecute(String response) {
            //here is your response
        }
    }

通过在InputStream上调用toString(),可以获得对象的哈希代码。试试这个:

Log.d("response", EntityUtils.toString(httpEntity));