-1

我在处理程序线程内的android服务中使用它来将数据发送到php页面以插入mysql数据库,url没问题但由于某种原因它没有发布到php页面.url在开头被定义为公共字符串班上 。

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 后烘烤的链接很好。这有什么问题。

4

2 回答 2

0

不是使用 URLConnection,而是构造一个HttpClient对象,以执行HttpPost使用您的目标 url 构造的对象。Jeff Vogella 在第 2.3 节中有一个非常好的例子:http ://www.vogella.com/articles/ApacheHttpClient/article.html

于 2013-02-15T21:25:31.843 回答
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
                    // ...
                }
于 2013-02-15T22:59:15.660 回答