2015-11-05 91 views
0

我正在编写Android应用程序的代码,应该采取经度和纬度,我想将它发布到Web服务器,但在此代码中,当我在我的Android手机上运行它将插入null在MySQL数据库中的值,我是在Android开发的新,所以好心帮我你的建议将higly赞赏提前 感谢HTTP POST双变量

package com.example.mypc.myapplication; 
    import android.location.Criteria; 
    import android.location.Location; 
    import android.location.LocationListener; 
    import android.location.LocationManager; 
    import android.os.AsyncTask; 
    import android.os.Bundle; 
    import android.app.Activity; 
    import android.content.Context; 
    import android.view.View; 
    import android.widget.TextView; 
    import android.view.Menu; 
    import android.widget.Toast; 

    import org.apache.http.HttpEntity; 
    import org.apache.http.HttpResponse; 
    import org.apache.http.NameValuePair; 
    import org.apache.http.client.ClientProtocolException; 
    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 java.io.IOException; 
    import java.util.ArrayList; 
    import java.util.List; 

    public class MainActivity extends Activity implements LocationListener{ 
     LocationManager lm; 
     TextView lt, ln; 
     String provider; 
     Location l; 
     @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); 
      //get location service 
      lm=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE); 
      Criteria c=new Criteria(); 
      //criteria object will select best service based on 
      //Accuracy, power consumption, response, bearing and monetary cost 
      //set false to use best service otherwise it will select the default Sim network 
      //and give the location based on sim network 
      //now it will first check satellite than Internet than Sim network location 
      provider=lm.getBestProvider(c, false); 
      //now you have best provider 
      //get location 
      l=lm.getLastKnownLocation(provider); 
      if(l!=null) 
      { 
       //get latitude and longitude of the location 
       double lng=l.getLongitude(); 
       double lat=l.getLatitude(); 
       //display on text view 
       ln.setText(""+lng); 
       lt.setText(""+lat); 
      } 
      else 
      { 
       ln.setText("No Provider"); 
       lt.setText("No Provider"); 
      } 
     } 
     //If you want location on changing place also than use below method 
     //otherwise remove all below methods and don't implement location listener 
     @Override 
     public void onLocationChanged(Location arg0) 
     { 
      double lng=l.getLongitude(); 
      double lat=l.getLatitude(); 
      ln.setText(""+lng); 
      lt.setText(""+lat); 
     } 
     public void insert(View view){ 
      String lati = lt.getText().toString(); 
      String longi = ln.getText().toString(); 

      insertToDatabase(lati,longi); 
     } 
     private void insertToDatabase(final String lt, final String ln){ 
      class SendPostReqAsyncTask extends AsyncTask<String, Void, String> { 
       @Override 
       protected String doInBackground(String... params) { 
        String paramlt = params[0]; 
        String paramln = params[1]; 


        List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>(); 
        nameValuePairs.add(new BasicNameValuePair("latitude",Double.toString(Double.parseDouble(lt)))); 
        nameValuePairs.add(new BasicNameValuePair("Longitude",Double.toString(Double.parseDouble(ln)))); 

        try { 
         HttpClient httpClient = new DefaultHttpClient(); 
         HttpPost httpPost = new HttpPost(
           "http://calcare.pk/insert.php"); 
         httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

         HttpResponse response = httpClient.execute(httpPost); 

         HttpEntity entity = response.getEntity(); 


        } catch (ClientProtocolException e) { 

        } catch (IOException e) { 

        } 
        return "success"; 
       } 
       @Override 
       protected void onPostExecute(String result) { 
        super.onPostExecute(result); 

        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show(); 
        TextView textViewResult = (TextView) findViewById(R.id.textViewResult); 
        textViewResult.setText("Inserted"); 
       } 
      } 
      SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask(); 
      sendPostReqAsyncTask.execute(lt, ln); 
     } 

     @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      // Inflate the menu; this adds items to the action bar if it is present. 
      getMenuInflater().inflate(R.menu.menu_main, menu); 
      return true; 
     } 
     @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 
     } 
    } 

我插入MySQL中的数据PHP脚本如下

<?php 
    $host='7.8.7.10'; 
    $uname='asdfa'; 
    $pwd='asdffp'; 
    $db="asdfasf"; 

    $con = mysql_connect($host,$uname,$pwd) or die("connection failed"); 
    mysql_select_db($db,$con) or die("db selection failed"); 

    $Latitude = $_POST['lati']; 
     $Longitude = $_POST['longi']; 

    $flag['code']=0; 

    if($r=mysql_query("insert into sample (latitude,Longitude) values ('$Latitude','$Longitude')",$con)) 
    { 
     $flag['code']=1; 
     echo"hi"; 
    } 

    print(json_encode($flag)); 
    mysql_close($con); 
?> 

回答

0
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask(); 
String[] str = new String[2]; 
str[0] = lt; 
str[1] = ln ; 
sendPostReqAsyncTask.execute(str); 
给出

doInBackground

nameValuePairs.add(new BasicNameValuePair("latitude",paramlt)); 
nameValuePairs.add(new BasicNameValuePair("Longitude",paramln)); 
+0

您回应可观,但#Parag肖汉又把它插入空值我的PHP脚本插入波纹管是

+0

我不知道PHP的一面,但你可以参考获取数据http://stackoverflow.com/questions/4685534/android-json-to-php-server-and-back –