值在文本视图中没有传递到Mysql数据库

问题描述:

在我的android应用程序中,我获取textview的经度和纬度,然后即时通讯尝试将它们传递给Mysql DB.MySql Db本地创建..我的数据库有3个字段ID(自动递增)的纬度和经度(双)。没有登录猫errores值在文本视图中没有传递到Mysql数据库

这里是我的主要活动

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); 
    ?> 
+3

使用的AsyncTask时许HTTP调用检查这个代码HTTP ://stackoverflow.com/questions/9671546/asynctask-android-example –

+0

使用AsyncTask在后台线程做网络动作 –

+0

@Anup Dasari你能告诉我,我需要从上面的代码编辑哪部分? – colombo

使用的AsyncTask是最好的网络operation.But你可以在你的oncreate方法添加此克服android.os.NetworkOnMainThreadException ..

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 

    StrictMode.setThreadPolicy(policy); 

这将删除你的android.os.NetworkOnMainThreadException 但最好的编程是使用asyncTask