我在处理程序线程内的android服务中使用这个将数据发送到php页面以插入到mysql数据库中,url是可以的但是由于某种原因它没有被发布到php页面.url被定义为Public String on班上的开始。
public void onCreate() {
super.onCreate();
// final String mojsadrzaj=new String();
Toast.makeText(this,"Service created ...",Toast.LENGTH_LONG).show();
final SQLiteDatabase db=openOrCreateDatabase("TruckMe", MODE_PRIVATE, null);
db.execSQL("create table if not exists lokacije (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, lokacija VARCHAR(600));");
//regulisanje podataka za logovanje samo prvi put
String idvozaca=new String();
db.execSQL("create table if not exists vozac (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, idvozaca VARCHAR(15));");
Cursor d=db.rawQuery("select idvozaca from vozac", null);
//db.execSQL("delete from vozac;");
if(d.getCount() > 0)
{
d.moveToLast();
idvozaca=d.getString(d.getColumnIndex("idvozaca"));
mojsadrzaj=idvozaca;
Toast t=Toast.makeText(this, "Id vozaca je : " + mojsadrzaj, Toast.LENGTH_LONG);
t.setGravity(Gravity.CENTER, 0, 0);
t.show();
db.close();
}
//regulisanje podataka za logovanje samo prvi put
final Time t=new Time();
//Toast.makeText(this,"Stao kod upita" , Toast.LENGTH_LONG).show();
//deo za thread
final Handler handler = new Handler();
final Runnable runnable = new Runnable() {
public void run() {
if(indikator != 0)
{
try {
URL myURL = new URL(url);
URLConnection myURLConnection = myURL.openConnection();
myURLConnection.connect();
Toast.makeText(MyService.this,"saljem na net "+url , Toast.LENGTH_LONG).show();
}
catch (MalformedURLException e) {
// new URL() failed
// ...
}
catch (IOException e) {
// openConnection() failed
// ...
}
}
indikator=1;
handler.postDelayed(this, 9000);
}
};
//b.setBackgroundColor(Color.RED);
//deo koji naknadno ubacujem za kriterijum
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
//kraj dela koji sam naknadno ubacio
final LocationManager m=(LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
//deo koji sam takodje ubacio naknadno
String locationprovider =m.getBestProvider(criteria,true);
//deo koji sam takodje ubacio naknadno kraj
LocationListener l=new LocationListener() {
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location arg0) {
String zauzetost=new String();
zauzetost="ZAUZET";
String format="MM/dd/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.US);
String date= sdf.format(new Date());
t.setToNow();
String longitude=new String();
String latitude=new String();
String speed=new String();
if((int)arg0.getSpeed()==0)
{
speed="0";
}
else
{
speed=""+(((int)arg0.getSpeed()*3600)/1000);
}
longitude=""+ arg0.getLongitude();
latitude=""+ arg0.getLatitude();
Calendar cal=Calendar.getInstance();
url="http://www.compensatemeonline.com/truckmeonline/TruckMeOnline/UnosLokacijaSaTelefona.php?id="+mojsadrzaj+"&longitude="+longitude+"&latitude="+latitude+"&brzina="+speed+"&vreme="+cal.getTime()+"&datum="+date+"&zauzetost="+zauzetost;
// TODO Auto-generated method stub
}
};
// TODO Auto-generated method stub
//pozivanje threada
runnable.run();
//pozivanje threada
m.requestLocationUpdates(locationprovider, 6000, 0, l);
// zamenio sam m.requestLocationUpdates(LocationManager.GPS_PROVIDER, 120000, 0, l); sa m.requestLocationUpdates(locationprovider, 120000, 0, l);
}
所有这些都可以在活动中完成并使用webview发布到php页面。但由于我无法使用webview服务,我使用URL ...,在调用url之后烘烤的链接很好。这有什么问题。
答案 0 :(得分:0)
不是使用URLConnection,而是构造HttpClient
对象,以执行使用目标URL构造的HttpPost
。 Jeff Vogella在第2.3节中有一个很好的例子:http://www.vogella.com/articles/ApacheHttpClient/article.html
答案 1 :(得分:0)
我使用了这个
try {
URL myURL = new URL(url);
URLConnection myURLConnection = myURL.openConnection();
myURLConnection.connect();
//
BufferedReader in = new BufferedReader(new InputStreamReader(
myURLConnection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
//
Toast.makeText(MyService.this,"saljem na net "+url , Toast.LENGTH_LONG).show();
}
catch (MalformedURLException e) {
// new URL() failed
// ...
}
catch (IOException e) {
// openConnection() failed
// ...
}