我使用AsyncTask发送多个参数,参数包含Gps数据和编辑文本(用户输入)电子邮件。
public class Camera extends Activity implements OnClickListener {
ImageButton ib;
Button savebutton;
static final int REQUEST_IMAGE_CAPTURE = 1;
private static final String TAG= "PostActivity";
EditText email;
LocationManager locationmanager;
Location location;
private String latlng = "";
Double lat;
Double lng;
private String longitude;
private String latitude;
private String gemail = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photo_layout);
String context = Context.LOCATION_SERVICE;
locationmanager = (LocationManager) getSystemService(context);
String provider = LocationManager.GPS_PROVIDER;
location = locationmanager.getLastKnownLocation(provider);
giveMeNewLocation(location);
TakePic();
//收集位置后立即调用的TakePic() } // giveMeNewLocation是一个方法,它为Location对象提供lat和long值
private void giveMeNewLocation(Location location) {
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
latitude= String.valueOf(lat);
longitude= String.valueOf(lng);
latlng = "Latitude:" + lat + "Longitude:" + lng;
Toast.makeText(this, latlng, Toast.LENGTH_LONG).show();
Log.i(TAG,"Post Locations to a Server..." +latlng);
}
}
private void TakePic() {
email = (EditText)findViewById(R.id.eText);
ib = (ImageButton) findViewById(R.id.ibTakePic);
savebutton = (Button) findViewById(R.id.bProceed);
ib.setOnClickListener(this);
}
}
public class MyTask extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... params) {
String alatlng = params[0];
String bgemail = params[1];
// TODO Auto-generated method stub
List<NameValuePair> p= new ArrayList<NameValuePair>();
p.add(new BasicNameValuePair("Locations", alatlng));
p.add(new BasicNameValuePair("Email", bgemail));
// HTTP Object to send Data
HttpClient client= new DefaultHttpClient();
HttpPost post= new HttpPost("http://myipadd/Examples/LatLng.php");
try {
//Put data in the object
post.setEntity(new UrlEncodedFormEntity(p));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Execute post Which provide a response
HttpResponse responcee = null;
try {
responcee = client.execute(post);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int status=responcee.getStatusLine().getStatusCode();
String nstatus=String.valueOf(status);
Log.i("Response From Server:", nstatus);
return nstatus;
}
protected void onPostExecute(String nstatus){
super.onPostExecute(nstatus);
Toast.makeText(getBaseContext(),"Post Response:" +nstatus, Toast.LENGTH_SHORT).show();
}
}
/*Explicit Intent to start Data Activity,
This is just another method of using a button without setting onClickListener,//
Instead we use the Method name in the Layout Xml file by adding android:onClick="CollectData"*/
public void CollectData(View v) {
gemail= email.getText().toString();
new MyTask().execute(latlng,gemail);
}
}
我使用的Php脚本:
<?php
$db_host = "localhost";
$db_uid = "xxxx";
$db_pass = "xxxx";
$db_name = "xxxx";
$db_con = mysql_connect($db_host,$db_uid,$db_pass) or die('could not connect');
mysql_select_db($db_name);
$latlngdata= $_POST['Locations'];
$emaildata= $_POST['Email'];
mysql_query("INSERT INTO locations(email, latlng) VALUES ('$emaildata','$latlngdata')");
?>