Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/184.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取Json对象上的空对象引用错误_Java_Android_Mysql - Fatal编程技术网

Java 获取Json对象上的空对象引用错误

Java 获取Json对象上的空对象引用错误,java,android,mysql,Java,Android,Mysql,Java文件: 我想通过android应用程序更新MySQl数据库中的现有行。我得到空指针异常。下面是我的代码和logcat。我试着把它修好,但没有成功。请帮忙。web服务在使用HTML运行时运行良好。但与android连接时无法工作 private ProgressDialog pDialog; JSONParser jsonParser = new JSONParser(); int success=0; double pickup_lat

Java文件:

我想通过android应用程序更新MySQl数据库中的现有行。我得到空指针异常。下面是我的代码和logcat。我试着把它修好,但没有成功。请帮忙。web服务在使用HTML运行时运行良好。但与android连接时无法工作

 private ProgressDialog pDialog;

        JSONParser jsonParser = new JSONParser();
        int success=0;
        double pickup_lat, pickup_lng, drop_lat, drop_lng;
        // url to create new product
        private static String url_create_product = "http://192.168.10.6:8080/courier/add_location.php";

        // JSON Node names
        private static final String TAG_SUCCESS = "success";
        private static final String TAG_USERNO = "user_no";
        private static final String TAG_PICKUP_LAT = "pickup_lat";
        private static final String TAG_PICKUP_LNG = "pickup_lng";
        private static final String TAG_DROP_LAT = "drop_lat";
        private static final String TAG_DROP_LNG = "drop_lng";

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

            /**
             * Before starting background thread Show Progress Dialog
             */
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(MapActivity.this);
                pDialog.setMessage("Adding Details..");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.show();
            }

            /**
             * Creating product
             */
            protected String doInBackground(String... args) {

                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
               params.add(new BasicNameValuePair(TAG_USERNO, "120"));
                params.add(new BasicNameValuePair(TAG_PICKUP_LAT, "12"));
                params.add(new BasicNameValuePair(TAG_PICKUP_LNG,  "12"));
                params.add(new BasicNameValuePair(TAG_DROP_LAT,  "12"));
                params.add(new BasicNameValuePair(TAG_DROP_LNG, "12"));


                // getting JSON Object
                // Note that create product url accepts POST method
                JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                        "POST", params);

                // check for success tag
                try {
                    success = json.getInt(TAG_SUCCESS);

                } 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 done
                pDialog.dismiss();
                if (success == 1) {
                    // successfully created product
                    Intent i = getIntent();
                    // send result code 100 to notify about product update
                    setResult(100, i);
                    finish();
                    // closing this screen

                    finish();
                } else {

                    //failed to create product
                }
            }
        }
        @Override
        public void onDestroy(){
            super.onDestroy();

            pDialog.dismiss();
            pDialog.cancel();
        }
    }
private ProgressDialog pDialog;
JSONParser JSONParser=新的JSONParser();
int成功=0;
双接收板、接收板、下降板、下降板;
//创建新产品的url
私有静态字符串url\u创建\u产品=”http://192.168.10.6:8080/courier/add_location.php";
//JSON节点名称
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串标记\u USERNO=“user\u no”;
专用静态最终字符串标记\u picku\u LAT=“picku\u LAT”;
专用静态最终管柱标签\u picku\u LNG=“picku\u LNG”;
私有静态最终字符串标记\u DROP\u LAT=“DROP\u LAT”;
专用静态最终字符串标记\u DROP\u LNG=“DROP\u LNG”;
类AddLocation扩展了异步任务{
/**
*在启动后台线程显示进度对话框之前
*/
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(MapActivity.this);
pDialog.setMessage(“添加详细信息”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
/**
*创造产品
*/
受保护的字符串doInBackground(字符串…args){
//建筑参数
List params=new ArrayList();
参数add(新的BasicNameValuePair(TAG_USERNO,“120”);
参数添加(新的BasicNameValuePair(标签“12”);
添加参数(新的BasicNameValuePair(标签“12”);
参数添加(新的BasicNameValuePair(标签“12”);
参数添加(新的BasicNameValuePair(标签“12”);
//获取JSON对象
//请注意,创建产品url接受POST方法
JSONObject json=jsonParser.makeHttpRequest(url\u create\u product,
“POST”,params);
//检查成功标签
试一试{
success=json.getInt(TAG_success);
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
/**
*完成后台任务后,关闭“进度”对话框
**/
受保护的void onPostExecute(字符串文件\u url){
//完成后关闭对话框
pDialog.disclose();
如果(成功==1){
//已成功创建产品
Intent i=getIntent();
//发送结果代码100以通知产品更新
setResult(100,i);
完成();
//关闭此屏幕
完成();
}否则{
//未能创建产品
}
}
}
@凌驾
公共空间{
super.ondestory();
pDialog.disclose();
pDialog.cancel();
}
}
Web服务代码:

<?php header('Access-Control-Allow-Origin: *'); ?>
<?php header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept'); ?>
<?php header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT'); ?>
<?php
// array for JSON response
$response = array();
if (isset($_POST['user_no']) && isset($_POST['pickup_lat']) && isset($_POST['pickup_lng']) && isset($_POST['drop_lat']) && isset($_POST['drop_lng'])) {
   $user_no = $_POST['user_no'];
    $pickup_lat = $_POST['pickup_lat'];
    $pickup_lng = $_POST['pickup_lng'];
    $drop_lat= $_POST['drop_lat'];
    $drop_lng = $_POST['drop_lng'];
     }

$db_host = "localhost"; 
// Place the username for the MySQL database here 
$db_username = "root"; 
// Place the password for the MySQL database here 
$db_pass = "";  
// Place the name for the MySQL database here 
$db_name = "smartcourier";

// Run the actual connection here  
$link=mysqli_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mysql");
mysqli_select_db($link,"$db_name") or die ("no database");

$sql = "UPDATE courier_detail SET pickup_lat='$pickup_lat' , pickup_lng='$pickup_lng',  drop_lat= '$drop_lat', drop_lng=$drop_lng WHERE user_no=$user_no";
$result =mysqli_query($link,$sql);

if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "Location added successfully.";

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

@mysql_close($link);

?>


这可能是因为jsonParser.makeHttpRequest()方法返回空对象,该对象被分配给json对象。这就是为什么在尝试从json对象获取int()时会出现空指针异常。请再次检查您的jsonParser.makeHttpRequest(),或者通过此方法检查jsonParser.makeHttpRequest()是否返回NOTNULL对象

            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                    "POST", params);

            // check if json is null or not
            Log.i("Json Check : ", json == null + "");

            // check for success tag
            try {
                success = json.getInt(TAG_SUCCESS);

            } catch (JSONException e) {
                e.printStackTrace();

            }

我已经检查了这些链接,但没有得到任何有用的在我的情况下。因此,我单独提出了问题,例如,检查
json
的值,意识到它是空的,并且正确无误,然后查看和/或调试
makeHttpRequest
中的代码,看看它为什么返回
null
,这是一项我们无法完成的工作,因为您没有共享该代码,而且它不是内置的Android。