Java 如何在Android中使用Post方法发布任何数据

Java 如何在Android中使用Post方法发布任何数据,java,android,location,http-post,Java,Android,Location,Http Post,请告诉我代码的位置。 我正在创建一个应用程序 获取位置 获取位置并单击“发布”按钮后 很快,使用POST(HTTP)方法将其发送到我的PHP页面 如下面的应用程序屏幕截图所示,您可以看到两个输入框,其中包含LAT、LONG和Name值: 我有以下代码来获取位置: public class MainActivity extends Activity implements LocationListener { EditText lat; EditText lng; @Override prote

请告诉我代码的位置。

我正在创建一个应用程序

  • 获取位置
  • 获取位置并单击“发布”按钮后
  • 很快,使用POST(HTTP)方法将其发送到我的PHP页面
  • 如下面的应用程序屏幕截图所示,您可以看到两个输入框,其中包含LAT、LONG和Name值:

    我有以下代码来获取位置:

    public class MainActivity extends Activity implements LocationListener {
    EditText lat;
    EditText lng;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        /********** get Gps location service LocationManager object ***********/
    
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    
                /* CAL METHOD requestLocationUpdates */
    
        // Parameters :
        //   First(provider)    :  the name of the provider with which to register
        //   Second(minTime)    :  the minimum time interval for notifications,
        //                         in milliseconds. This field is only used as a hint
        //                         to conserve power, and actual time between location
        //                         updates may be greater or lesser than this value.
        //   Third(minDistance) :  the minimum distance interval for notifications, in meters
        //   Fourth(listener)   :  a {#link LocationListener} whose onLocationChanged(Location)
        //                         method will be called for each location update
    
    
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                3000,   // 3 sec
                10, this);
    
        /********* After registration onLocationChanged method  ********/
        /********* called periodically after each 3 sec ***********/
    }
    
    /************* Called after each 3 sec **********/
    @Override
    public void onLocationChanged(Location location) {
    
        lat = (EditText)findViewById(R.id.lat);
        lng = (EditText)findViewById(R.id.lng);
        lat.setText(""+location.getLatitude());
        lng.setText(""+location.getLongitude());
    
        String str = "Latitude: "+location.getLatitude()+"Longitude: "+location.getLongitude();
    
        Toast.makeText(getBaseContext(), str, Toast.LENGTH_LONG).show();
    }
    
    
    @Override
    public void onProviderDisabled(String provider) {
    
        /******** Called when User off Gps *********/
    
        Toast.makeText(getBaseContext(), "GPS is OFF, Turn it on", Toast.LENGTH_LONG).show();
    }
    
    @Override
    public void onProviderEnabled(String provider) {
    
        /******** Called when User on Gps  *********/
    
        Toast.makeText(getBaseContext(), "GPS ON ", Toast.LENGTH_LONG).show();
        Toast.makeText(getBaseContext(), "Waiting for location..... ", Toast.LENGTH_LONG).show();
    }
    
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
    
    }
    
    
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    }    
    
    这是当GPS获得定位时,获取位置并将LAT和LONG值填入输入框的代码

    现在,,
    我需要将所有数据发布到我的PHP页面中的代码。

    如果有帮助的话。这并不是运行以下代码所需的全部异步任务。我希望您知道,为网络启用清单中的权限。 i、 e:

    现在data是需要发送lat long数据的字符串

    还有另一种向服务器发送数据的方法

       public HttpResponse YourMethod(String lat,String lon,Strig url) {
             HttpResponse response =null;
               // Create a new HttpClient and Post Header
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new    HttpPost(url);
    
         try {
    
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair(<lat key from php>, lat));
        nameValuePairs.add(new BasicNameValuePair(<lat key from php>, lon));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    
    
        HttpResponse response = httpclient.execute(httppost);
    
         } catch (Exception e) {
    
       }
        finally{
           return response 
          }
    
     } 
    
    public-HttpResponse-YourMethod(字符串lat、字符串lon、Strig-url){
    HttpResponse响应=null;
    //创建一个新的HttpClient和Post头
    HttpClient HttpClient=新的DefaultHttpClient();
    HttpPost HttpPost=新的HttpPost(url);
    试一试{
    List nameValuePairs=新的ArrayList(2);
    添加(新的BasicNameValuePair(,lat));
    添加(新的BasicNameValuePair(,lon));
    setEntity(新的UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response=httpclient.execute(httppost);
    }捕获(例外e){
    }
    最后{
    返回响应
    }
    } 
    
    查看此链接将值从android应用程序发送到php页面可能会有所帮助,,,,,,,,,,,,,,,,,,,,我们首先需要知道php端点接受什么-它是否获得JSON?它期望什么属性?如果它需要一个令牌或其他身份验证,您是否已经知道如何做到这一点?略去一些细节,比如URL,但是细节对于一个好的响应来说很重要。也可以使用这个lnk信息来获取关于Post方法和PHP服务的knwoladge。。谢谢,但我一点也不懂。什么是php中的lat键,YourMethod它是键。第二种方法是名称-值对,其中提供一个键及其对应的value@Kaushik:键是否与HTML中的“name”属性类似?@RaviPrakash请通过接收lat lon的php页面代码。准备好了吗?@Kaushik:不,先生,对不起,我弄错了。。。。。。更正后我会回复:)
       public HttpResponse YourMethod(String lat,String lon,Strig url) {
             HttpResponse response =null;
               // Create a new HttpClient and Post Header
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new    HttpPost(url);
    
         try {
    
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair(<lat key from php>, lat));
        nameValuePairs.add(new BasicNameValuePair(<lat key from php>, lon));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    
    
        HttpResponse response = httpclient.execute(httppost);
    
         } catch (Exception e) {
    
       }
        finally{
           return response 
          }
    
     }