【发布时间】:2015-04-09 02:24:44
【问题描述】:
请告诉我在哪里放置代码。
我正在创建一个应用程序,
- 获取位置,
- 获得位置后,点击发布按钮,
- 很快,使用 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);
}
}
这是获取位置并将 LAT 和 LONG 值填充到输入框的代码,当 GPS 得到修复时。
现在, 我需要 POST 将所有数据写入我的 PHP 页面的代码。
【问题讨论】:
-
查看此链接可能有助于将值从 android 应用程序发送到 php 页面 adilmukarram.wordpress.com/2011/01/29/… ,,,,,,,,,,,,,,,,,,,anujarosha.wordpress.com/2012/01/27/…
-
我们首先需要知道 PHP 端点接受什么——它是在获取 JSON 吗?它期望什么属性?如果它需要令牌或其他身份验证,您是否已经知道该怎么做?省略 URL 等细节,但细节对于良好的响应很重要。
-
使用此 lnk 信息获取有关 Post 方法和 PHP 服务的知识。androidhive.info/2011/10/android-making-http-requests
标签: java android location http-post