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!
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 ?