douxiangbiao1899 2013-07-31 19:12
浏览 52
已采纳

具有多个用户位置的Google Maps Android应用

I am attempting to make an app where multiple users send their current location to a remote MySQL database and then each user can pull the lat, long coords to see all users on the map. The problem I am having at the moment is that when I try to send my location to the MySQL database my app crashes. Any help would be greatly appreciated. Here is my java activity:

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

I am fairly certain the error is occurring in the "Attempt Store" class as clicking on the bStore button creates the error

This is the logcat:

   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!

The error is the JSON parser cannot parse the data. I know my PHP scripts are correct because i can send and receive data by just loading them in my browser. Any help would be super cool

Thanks!

  • 写回答

1条回答 默认 最新

  • doomm4711 2013-07-31 19:43
    关注

    have you check the json format

    you have this error

    Error parsing data org.json.JSONException: Value

    so check in response you get json object or json array ?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗