0

我正在尝试制作一个应用程序,其中多个用户将他们当前的位置发送到远程 MySQL 数据库,然后每个用户都可以拉取经纬度、经度坐标以查看地图上的所有用户。我目前遇到的问题是,当我尝试将我的位置发送到 MySQL 数据库时,我的应用程序崩溃了。任何帮助将不胜感激。这是我的java活动:

   package com.paul.locations;

   import java.text.SimpleDateFormat;
   import java.util.ArrayList;
   import java.util.Calendar;
   import java.util.List;

   import org.apache.http.NameValuePair;
   import org.apache.http.message.BasicNameValuePair;
   import org.json.JSONException;
   import org.json.JSONObject;

   import com.paul.locations.JSONParser;
   import com.paul.locations.R;
   import com.google.android.gms.maps.GoogleMap;
   import com.google.android.gms.maps.MapFragment;

   import android.location.LocationManager;
   import android.os.AsyncTask;
   import android.os.Bundle;
   import android.app.Activity;
   import android.app.ProgressDialog;
   import android.content.Context;
   import android.util.Log;
   import android.view.View;
   import android.view.View.OnClickListener;
   import android.widget.Button;


   public class Location extends Activity implements OnClickListener {
private GoogleMap googlemap;
private String lat="", lon="", current = "";
private Button  bLoad, bStore;

 // Progress Dialog
private ProgressDialog pDialog;

// JSON parser class
JSONParser jsonParser = new JSONParser();

//php add a comment script

//localhost :  
//testing on your device
//put your local ip instead,  on windows, run CMD > ipconfig
//or in mac's terminal type ifconfig and look for the ip under en0 or en1
   private static final String LOAD_INFO_URL = "http://192.168.1.14/locations/load_info.php";
  private static final String UPDATE_INFO_URL = "http://192.168.1.14/locations/update_info.php";


//testing on Emulator:
//private static final String POST_COMMENT_URL = "http://10.0.2.2:1234/webservice/addcomment.php";

//testing from a real server:
//private static final String POST_COMMENT_URL = "http://www.mybringback.com/webservice/addcomment.php";

//ids
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        setContentView(R.layout.map);
        setUpMap();

    bLoad = (Button)findViewById(R.id.bLoad);
    bLoad.setOnClickListener(this);
    bStore = (Button)findViewById(R.id.bStore);
    bStore.setOnClickListener(this);        
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.bLoad:

        new AttemptLoad().execute();
        break;
    case R.id.bStore:
        LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
        android.location.Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        lon = Double.toString(location.getLongitude());
        lat = Double.toString(location.getLatitude());
        System.out.println(lon + ", " + lat);
        Calendar c = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
        current = df.format(c.getTime());
        System.out.println(current);
        new AttemptStore().execute();
        break;

    default:
        break;
    }
}

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

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Location.this);
        pDialog.setMessage("Attempting to load locations...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        return null;
    }       
}

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

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Location.this);
        pDialog.setMessage("Attempting to store your location...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        String success;
        try {
            // Building Parameters
            List<NameValuePair> params1 = new ArrayList<NameValuePair>();
            params1.add(new BasicNameValuePair("lat", lat));
            params1.add(new BasicNameValuePair("lon", lon));
            params1.add(new BasicNameValuePair("time", current));

            Log.d("request!", "starting");

            //Posting user data to script 
            JSONObject json = jsonParser.makeHttpRequest(
                    UPDATE_INFO_URL, "POST", params1);

            // full json response
            Log.d("Store location attempt", json.toString());

            // json success element
            success = json.getString(TAG_SUCCESS);
            if(success.equals("SUCCESS")) {
                Log.d("Location Stored!", json.toString());    
                finish();
                return json.getString(TAG_MESSAGE);
            }else{
                Log.d("Storing Location Failure!", json.getString(TAG_MESSAGE));
                return json.getString(TAG_MESSAGE);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }           
        return null;
    }

    protected void onPostExecute(String file_url) {
        // dismiss the dialog once product deleted
        pDialog.dismiss();
        //if (file_url != null){
            //Toast.makeText(Location.this, file_url, Toast.LENGTH_LONG).show();
        //}
    }
}

private void setUpMap() {
    if(googlemap == null){
        googlemap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

        if(googlemap != null){
            //code to initialize the map
            googlemap.setMyLocationEnabled(true);
        }
    }
}   
    }

我相当确定错误发生在“尝试存储”类中,因为单击 bStore 按钮会创建错误

这是日志:

   07-31 13:27:40.245: D/SensorManager(30830): unregisterListener::  Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41d55290
   07-31 13:27:40.245: I/SensorManager(30830): Set normal delay = true
   07-31 13:27:40.245: D/SensorManager(30830): registerListener :: handle = 4  name= MPL accel delay= 200000 Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41d58410
   07-31 13:27:40.255: W/IInputConnectionWrapper(30830): getSelectedText on inactive InputConnection
   07-31 13:27:40.265: W/IInputConnectionWrapper(30830): setComposingText on inactive InputConnection
   07-31 13:27:43.779: D/request!(30830): starting
   07-31 13:27:43.799: I/Adreno200-EGLSUB(30830): <ConfigWindowMatch:2087>: Format RGBA_8888.
   07-31 13:27:43.829: D/SensorManager(30830): unregisterListener::  Listener= android.view.OrientationEventListener$SensorEventListenerImpl@41d58410
   07-31 13:27:43.829: I/SensorManager(30830): Set normal delay = true
   07-31 13:27:45.241: D/Login attempt(30830): {"message":"Login successful!","success":1}
   07-31 13:27:45.241: D/Login Successful!(30830): {"message":"Login successful!","success":1}
   07-31 13:27:45.661: D/AbsListView(30830): Get MotionRecognitionManager
   07-31 13:27:45.771: I/Adreno200-EGLSUB(30830): <ConfigWindowMatch:2087>: Format RGBA_8888.
   07-31 13:27:45.801: I/Adreno200-EGLSUB(30830): <ConfigWindowMatch:2098>: Format RGBX_8888.
   07-31 13:27:45.961: W/IInputConnectionWrapper(30830): getSelectedText on inactive InputConnection
   07-31 13:27:45.961: W/IInputConnectionWrapper(30830): setComposingText on inactive InputConnection
   07-31 13:27:49.765: I/System.out(30830): -117.21201925, 32.75091863
   07-31 13:27:49.765: I/System.out(30830): 13:27:49
   07-31 13:27:49.796: D/request!(30830): starting
   07-31 13:27:49.816: I/Adreno200-EGLSUB(30830): <ConfigWindowMatch:2087>: Format RGBA_8888.
   07-31 13:27:50.987: E/JSON Parser(30830): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
   07-31 13:27:50.987: D/Store location attempt(30830): {"message":"Login successful!","success":1}
   07-31 13:27:50.987: D/Storing Location Failure!(30830): Login successful!

错误是 JSON 解析器无法解析数据。我知道我的 PHP 脚本是正确的,因为我可以通过在浏览器中加载它们来发送和接收数据。任何帮助都会非常酷

谢谢!

4

1 回答 1

1

你检查过json格式吗

你有这个错误

解析数据时出错 org.json.JSONException:值

所以检查你得到 json 对象或 json 数组的响应?

于 2013-07-31T19:43:20.083 回答