2013-02-15 119 views
-1

我在android服務裏面使用這個處理程序線程來發送數據到php頁面插入到mysql數據庫中,url是好的,但是由於某些原因它不會被髮布到php頁面.url是在類的開頭定義爲公共字符串。發送數據到php頁面

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

作爲旁工作,默認情況下在主線程上運行的服務,所以你要移動的數據庫和網絡運營到單獨的線程。 – 2013-02-15 20:19:03

+0

性能不錯,但不是主要問題。爲什麼我的php頁面沒有執行? – 2013-02-15 20:22:07

+0

我看到你打開了連接,但沒有得到一個outputStream並且寫了任何東西。如果你只需要做一個簡單的帖子,HttpClient類可能更適合你的需求。 – 2013-02-15 20:26:27

回答

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 
        // ... 
       } 
+0

我注意到的是,網站鏈接字符串的大小的大小是造成問題的大小! – 2013-02-16 00:21:55

+0

主要問題是,我有一些空間標誌的鏈接,刪除它們後,它工作正常! – 2013-02-16 12:46:19

0

代替使用URLConnection,構造一個HttpClient對象,執行使用目標url構造的HttpPost。傑夫Vogella在這裏有2.3節一個很好的例子:http://www.vogella.com/articles/ApacheHttpClient/article.html

+0

使用該方法我得到一個錯誤02-15 21:01:00.169:E/AndroidRuntime(1880):java.lang.IllegalArgumentException:在索引154查詢中的非法字符:http://www.compensatemeonline.com/truckmeonline/ TruckMeOnline/UnosLokacijaSaTelefona.php?ID = 16&經度= -122.084095與緯度= 37.422005&brzina = 0&vreme =週五02月15日21點00分56秒格林尼治標準時間2013&基準= 02/15/2013&zauzetost = ZAUZET \t \t \t \t \t \t} – 2013-02-15 22:15:08

+0

和我的服務我已經在活動中成功地使用過這幾次,但不知道現在的問題是什麼。也許我應該更改並使用一些特殊字母(當您使用URL方法時不需要)登錄鏈接。 – 2013-02-15 22:18:14