问题描述
我知道这个问题是问了很多次这样了,我试图按照20网页内容..
page 1
page 2
<一href=\"http://androidexample.com/Restful_Webservice_Call_And_Get_And_Parse_JSON_Data-_Android_Example/index.php?view=article_discription&aid=101&aaid=123\"相对=nofollow>第3页
page 4
等等等等...页
I know that this question is asked many times in SO,and I've tried to follow 20 web page content..page 1page 2page 3page 4etc etc pages...
基本上我的code 是Androidhive.info
Basically my code source is Androidhive.info
我的问题在日志猫is--它显示的位置,但它不是我的数据库中保存。
My issue is-- in log cat it is displaying the location but it is not saving in my database..
我在服务器上的PHP文件,我用的 httppost 按钮click..My座右铭调用它
I have a php file at server and I am calling it by httppost on button click..My motto is
1.On按钮单击它应该发送位置(这个任务完成)
1.On button click it should send the location(this task completed)
2.App应该后台运行,并在时间间隔发送位置信息(任务待定)
2.App should run in background and send location details in time intervals(task pending)
所建议的,所以我已经超期服役,但我在这里不使用任何报警经理或作业调度程序
As suggested in SO I've extended service but I am not using any alarm manager or job scheduler here
这是使我的code笨拙。
It is making my code clumsy..
所以的我的问题是的 C $ C必须在onLocationchanged方法写的东西$,这样它会发送数据到服务器时位置改变(程序将每运行n分钟)
So my question is what code should be written in onLocationchanged method so that it will send data to server whenever location is changed(program will run for every n minutes)
code片段:
1.GPSTracker.java
1.GPSTracker.java
public class GPSTracker extends Service implements LocationListener
{
statements;
}
在GPSTracker.java 2。有些部分
2.some part in GPSTracker.java
public void onLocationChanged(Location location)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0, this);
Log.e("onLocationChanged",Double.toString(latitude));
Log.e("onLocationChanged",Double.toString(longitude));
}
3.MainActivity.java
3.MainActivity.java
btnShowLocation.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0)
{
new LongOperation().execute();
}
}
4.MainActivity.java
4.MainActivity.java
private class LongOperation extends AsyncTask<String, Void, Void>
{
protected void onPreExecute()
{
}
protected Void doInBackground(String... urls)
{
ArrayList<NameValuePair> paramss = new ArrayList<NameValuePair>();
paramss.add(new BasicNameValuePair("latti", latti));
paramss.add(new BasicNameValuePair("longi", longi));
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http:///insertData.php");
httppost.setEntity(new UrlEncodedFormEntity(paramss));
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)
{
FLAG=1;
}
else
{
FLAG=0;
}
}
catch(Exception e)
{
Log.e("Fail 3", e.toString());
}
return null;
}
}
任何建议,将有很大的帮助。
Any suggestion would be of great help..
如果需要更多的code段请告知
Kindly inform if any more code snippets are required
我在做这个项目的Android工作室
I am doing this project in Android studio
LOG:
02-02 10:52:22.180 15792-15792/com.example.mylocation E/onLocationChanged﹕ 17.3732771
02-02 10:52:22.180 15792-15792/com.example.mylocation E/onLocationChanged﹕ 78.5046526
02-02 10:52:24.050 15792-16248/com.example.mylocation E/pass 1﹕ connection success
02-02 10:52:24.070 15792-16248/com.example.mylocation E/pass 2﹕ connection success
02-02 10:53:22.130 15792-15792/com.example.mylocation E/onLocationChanged﹕ 17.3733159
02-02 10:53:22.130 15792-15792/com.example.mylocation E/onLocationChanged﹕ 78.5045958
02-02 10:54:22.070 15792-15792/com.example.mylocation E/onLocationChanged﹕ 17.3733133
02-02 10:54:22.070 15792-15792/com.example.mylocation E/onLocationChanged﹕ 78.5046106
02-02 10:55:22.120 15792-15792/com.example.mylocation E/onLocationChanged﹕ 17.373314
02-02 10:55:22.120 15792-15792/com.example.mylocation E/onLocationChanged﹕ 78.5046077
这是给我log..but该数据位置更新每分钟不发送到数据库
every minute it is giving me the location update in log..but this data is not sent to database
我已经写了HTTP调用在doInBackground()这样的控制是不是传递给该方法......它永远不会再次调用。
I've written http call in doInBackground() so the control is not passing to that method...it is never called again..
修改1
当我用挂起的意图,此屏幕的输出,即使启用我的GPS设置
when I used pending intent this screen is the output,even though my GPS settings are enabled
加code:
public Location getLocation()
{
try
{
intent = new Intent("com.example.dotweb.mylocation");
pi = PendingIntent.getBroadcast(getApplicationContext(),0, intent, 0);
locationManager = (LocationManager) mContext
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
} else {
this.canGetLocation = true;
if (isNetworkEnabled)
{
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,60000,0, this);
Log.d("Network", "Network");
if (locationManager != null)
{
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isGPSEnabled) {
if (location == null)
{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,60000,0, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
既不 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,60000,0,这一点);
也不 locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,60000,0,PI);
正在做的伎俩
推荐答案
onLocationChanged内容。
@Override
public void onLocationChanged(Location location)
{
latitude = location.getLatitude();
longitude = location.getLongitude();
new LongOperation(latitude, longitude).execute();
}
LongOperation
是的AsyncTask类
有 HTTP调用
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("www.abc.com/kkk/ppp/nnn/update.php");
httppost.setEntity(new UrlEncodedFormEntity(paramss));
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());
}
这篇关于应该是什么onLocationChanged内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!