Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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
Android 无法分析Json解析器异常_Android_Json_Parsing - Fatal编程技术网

Android 无法分析Json解析器异常

Android 无法分析Json解析器异常,android,json,parsing,Android,Json,Parsing,此代码正在打印我的json解析器异常。 在这段代码中,我想向服务器发送纬度,但它会继续显示两个错误: 无法分析Json异常 异步任务#1错误 你能告诉我我做错了什么吗 这是我的相关代码: package com.example.gpstracking; public class AndroidGPSTrackingActivity extends Activity { Button btnShowLocation; String lat = "";

此代码正在打印我的json解析器异常。
在这段代码中,我想向服务器发送纬度,但它会继续显示两个错误:

  • 无法分析Json异常
  • 异步任务#1错误
  • 你能告诉我我做错了什么吗

    这是我的相关代码:

                 package com.example.gpstracking;
    
    
          public class AndroidGPSTrackingActivity extends Activity {
    
    Button btnShowLocation;
    
    String lat = "";
    String lng = "";
    // GPSTracker class
    GPSTracker gps;
    private ProgressDialog pDialog;
    
    JSONParser jsonParser = new JSONParser();
    // JSONParser jsonParser = new JSONParser();
    private static String url_create_product = "http://dyandroidapps.netii.net/"
            + "android_db/DB_Functncreate1.php";
    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        btnShowLocation = (Button) findViewById(R.id.btnShowLocation);
    
        // show location button click event
        btnShowLocation.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // create class object
                gps = new GPSTracker(AndroidGPSTrackingActivity.this);
    
                // check if GPS enabled
                if (gps.canGetLocation()) {
    
                    double latitude = gps.getLatitude();
                    double longitude = gps.getLongitude();
                     lat = Double.toString(latitude);
                     lng = Double.toString(longitude);
    
                    Toast.makeText(
                            getApplicationContext(),
            "Your Location is " + "- \nLat: " +  latitude
                + "\nLong: " + longitude, Toast.LENGTH_LONG)
                            .show();
                    new CreateNewProduct().execute();
                    // postData(lat, lng);
    
                } else {
                    // can't get location
                    // GPS or Network is not enabled
                    // Ask user to enable GPS/network in settings
                    gps.showSettingsAlert();
                }
    
            }
        });
    }
    
    /*
     * public void postData(String lat, String lng) { // Create a new HttpClient
     * and Post Header HttpClient httpclient = new DefaultHttpClient();
     * 
     * HttpPost httppost = new
     * HttpPost("http://dyandroidappa.netii.net/android_db/create_product.php");
     * 
     * try {
     * 
     * // Execute HTTP Post Request // HttpResponse response =
     * httpclient.execute(htget); HttpResponse response =
     * httpclient.execute(httppost); // String resp =
     * response.getStatusLine().toStrinkatyg(); try this // now //
     * Toast.makeText(this, resp, 5000).show();
     * 
     * } catch (ClientProtocolException e) { Toast.makeText(this, "Error",
     * 5000).show(); } catch (IOException e) { Toast.makeText(this, "Error",
     * 5000).show(); } }
     */
    
    class CreateNewProduct extends AsyncTask<String, String, String> {
    
        // * Before starting background thread Show Progress Dialog
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(AndroidGPSTrackingActivity.this);
            pDialog.setMessage("Sending markers..");
            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("lat", lat));
            params.add(new BasicNameValuePair("lng", lng));
    
            // params.add(new BasicNameValuePair("description", description));
    
            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_create_product,
                    "POST", params);
    
            // check log cat from response
            Log.d("Create Response", json.toString());
    
            // check for success tag
            try {
                int success = json.getInt(TAG_SUCCESS);
    
                if (success == 1) {
                    // successfully created product
                    Toast.makeText(getApplicationContext(), "Data Posted",
                            Toast.LENGTH_SHORT).show();
    
                    // closing this screen
                    finish();
                } else {
                    // failed to create product
                    Toast.makeText(getApplicationContext(), "Data not Posted",
                            Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Json Exception",
                        Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Only Exception",
                        Toast.LENGTH_SHORT).show();
    
            }
    
            return null;
        }
    
        // After completing background task Dismiss the progress dialog
    
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once done
            pDialog.cancel();
        }
    }
    
    }
    

    我不确定,但问题可能在于:

        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("lat", lat));
        params.add(new BasicNameValuePair("lng", lng));
    
    因此,这两种情况始终是相同的,正如开头所说:

    String lat = "";
    String lng = "";
    

    除此之外,在131行中还有一个NullPointerException。请发布。

    请发布您的实际响应。AndroidGPSTrackingActivity.java中的第131行是什么?响应代码的意思是。。。。。。?我没有其他代码,除了一个是posted谢谢你的评论我没有响应代码请建议我我是android新手,所以,碰巧这些lat,lng没有评论,它是由我评论的,只是为了测试,当它给我空指针异常,然后我来此,传递另一个值,并发布相同的代码,但在实际代码中,它不会被注释。感谢您现在告诉我您能提供更多帮助吗………您需要向我们提供您的代码,从第131行(AndroidGPSTrackingActivity.java:131行)开始,您有一个空指针异常,这意味着您正在访问一些不存在的东西。
    package com.example.gpstracking;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    import android.app.AlertDialog;
    import android.app.Service;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.os.IBinder;
    import android.provider.Settings;
    import android.util.Log;
    
    public class GPSTracker extends Service implements LocationListener {
    
        private final Context mContext;
    
        // flag for GPS status
        boolean isGPSEnabled = false;
    
        // flag for network status
        boolean isNetworkEnabled = false;
    
        // flag for GPS status
        boolean canGetLocation = false;
    
        Location location; // location
        double latitude; // latitude
        double longitude; // longitude
    
        // The minimum distance to change Updates in meters
        private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
    
        // The minimum time between updates in milliseconds
        private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
    
        // Declaring a Location Manager
        protected LocationManager locationManager;
    
        public GPSTracker(Context context) {
            this.mContext = context;
            getLocation();
        }
    
        public Location getLocation() {
            try {
                locationManager = (LocationManager) mContext
                        .getSystemService(LOCATION_SERVICE);
    
                // getting GPS status
                isGPSEnabled = locationManager
                        .isProviderEnabled(LocationManager.GPS_PROVIDER);
    
                // getting network status
                isNetworkEnabled = locationManager
                        .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    
                if (!isGPSEnabled && !isNetworkEnabled) {
                    // no network provider is enabled
                } else {
                    this.canGetLocation = true;
                    if (isNetworkEnabled) {
                        locationManager.requestLocationUpdates(
                                LocationManager.NETWORK_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("Network", "Network");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                    // if GPS Enabled get lat/long using GPS Services
                    if (isGPSEnabled) {
                        if (location == null) {
                            locationManager.requestLocationUpdates(
                                    LocationManager.GPS_PROVIDER,
                                    MIN_TIME_BW_UPDATES,
                                    MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                            Log.d("GPS Enabled", "GPS Enabled");
                            if (locationManager != null) {
                                location = locationManager
                                        .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                                if (location != null) {
                                    latitude = location.getLatitude();
                                    longitude = location.getLongitude();
                                }
                            }
                        }
                    }
                }
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return location;
        }
    
        /**
         * Stop using GPS listener Calling this function will stop using GPS in your
         * app
         * */
        public void stopUsingGPS() {
            if (locationManager != null) {
                locationManager.removeUpdates(GPSTracker.this);
            }
        }
    
        /**
         * Function to get latitude
         * */
        public double getLatitude() {
            if (location != null) {
                latitude = location.getLatitude();
            }
    
            // return latitude
            return latitude;
        }
    
        /**
         * Function to get longitude
         * */
        public double getLongitude() {
            if (location != null) {
                longitude = location.getLongitude();
            }
    
            // return longitude
            return longitude;
        }
    
        /**
         * Function to check GPS/wifi enabled
         * 
         * @return boolean
         * */
        public boolean canGetLocation() {
            return this.canGetLocation;
        }
    
        /**
         * Function to show settings alert dialog On pressing Settings button will
         * lauch Settings Options
         * */
        public void showSettingsAlert() {
            AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
    
            // Setting Dialog Title
            alertDialog.setTitle("GPS is settings");
    
            // Setting Dialog Message
            alertDialog
                    .setMessage("GPS is not enabled. Do you want to go to settings menu?");
    
            // On pressing Settings button
            alertDialog.setPositiveButton("Settings",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(
                                    Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            mContext.startActivity(intent);
                        }
                    });
    
            // on pressing cancel button
            alertDialog.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
    
            // Showing Alert Message
            alertDialog.show();
        }
    
        @Override
        public void onLocationChanged(Location location) {
    
        }
    
        @Override
        public void onProviderDisabled(String provider) {
        }
    
        @Override
        public void onProviderEnabled(String provider) {
        }
    
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    
        @Override
        public IBinder onBind(Intent arg0) {
            return null;
        }
    
    
    
        }
    
    }
    
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("lat", lat));
        params.add(new BasicNameValuePair("lng", lng));
    
    double latitude = gps.getLatitude();
    double longitude = gps.getLongitude();
    // lat = Double.toString(latitude);
    // lng = Double.toString(longitude);
    
    String lat = "";
    String lng = "";