在我的Android应用程序中,我获取textview的经度和经度然后我试图将它们传递给Mysql DB.MySql Db已经创建了本地..我的db有3个字段id(自动递增)纬度和经度(double) 。没有记录猫错误
这是我的主要活动
package com.example.newgps;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements LocationListener{
LocationManager lm;
TextView lt, ln;
String provider;
Location l;
int code;
InputStream is=null;
String result=null;
String line=null;
Button button1;
String latitude;
String longitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ln=(TextView)findViewById(R.id.lng);
lt=(TextView)findViewById(R.id.lat);
Button button1=(Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
latitude= lt.getText().toString();
longitude = ln.getText().toString();
insert();
}
});
lm=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
Criteria c=new Criteria();
provider=lm.getBestProvider(c, false);
l=lm.getLastKnownLocation(provider);
if(l!=null)
{
//get latitude and longitude of the location
double lng=l.getLongitude();
double lat=l.getLatitude();
ln.setText(""+lng);
lt.setText(""+lat);
}
else
{
ln.setText("No Provider");
lt.setText("No Provider");
}
}
@Override
public void onLocationChanged(Location arg0)
{
double lng=l.getLongitude();
double lat=l.getLatitude();
ln.setText(""+lng);
lt.setText(""+lat);
}
public void insert ()
{
final ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("latitude",latitude));
nameValuePairs.add(new BasicNameValuePair("longitude",longitude));
class RetrieveFeedTask extends AsyncTask<String, Void, String> {
private Exception exception;
protected String doInBackground(String... urls) {
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.31/gpstracking/insert.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("pass 1", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 1", e.toString());
Toast.makeText(getApplicationContext(), "Invalid IP Address",
Toast.LENGTH_LONG).show();
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.e("pass 2", "connection success ");
}
catch(Exception e)
{
Log.e("Fail 2", e.toString());
}
try
{
JSONObject json_data = new JSONObject(result);
code=(json_data.getInt("code"));
if(code==1)
{
Toast.makeText(getBaseContext(), "Inserted Successfully",
Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getBaseContext(), "Sorry, Try Again",
Toast.LENGTH_LONG).show();
}
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}
return latitude;
}
}}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
这是我的Php脚本
<?php
$host='localhost';
$uname='root';
$pwd='';
$db="gps";
$con = mysql_connect($host,$uname,$pwd) or die("connection failed");
mysql_select_db($db,$con) or die("db selection failed");
$latitude=$_REQUEST['latitude'];
$longitude=$_REQUEST['longitude'];
$flag['code']=0;
if($r=mysql_query("insert into location values('$latitude','$longitude') ",$con))
{
$flag['code']=1;
echo"hi";
}
print(json_encode($flag));
mysql_close($con);
?>
答案 0 :(得分:0)
使用asyncTask最适合网络操作。但您可以通过getFullName: function() {
return this.firstname + ' ' + this.lastname;
}
方法添加此功能来克服android.os.NetworkOnMainThreadException
..
oncreate
这将删除您的StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
但最好的编程是使用android.os.NetworkOnMainThreadException