【问题标题】:Send data to php page向php页面发送数据
【发布时间】:2013-02-15 20:06:18
【问题描述】:

我在处理线程内的 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 后烘烤的链接很好。这是什么问题。

【问题讨论】:

  • 顺便说一句,服务默认在主线程上运行,因此您希望将数据库和网络操作移至单独的线程。
  • 性能没问题,但不是主要问题。为什么我的php页面没有执行?
  • 我看到你打开了连接,但没有得到一个 outputStream 并向它写入任何内容。如果您只需要做一个简单的帖子,HttpClient 类可能更适合您的需求。
  • developer.android.com/reference/java/net/HttpURLConnection.html 在“发布内容”部分下是获取连接的输出流并将其写入的示例。
  • 但我只想调用一个页面,我使用 myURLConnection.connect();我不需要页面的内容,因为它只是将位置插入到数据库中的 php-mysql - 稍后在 . 上读取

标签: php android mysql sqlite


【解决方案1】:

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

【讨论】:

  • 使用这种方法我得到一个错误 02-15 21:01:00.169: E/AndroidRuntime(1880): java.lang.IllegalArgumentException: Illegal character in query at index 154: compensatemeonline.com/truckmeonline/TruckMeOnline/… Feb 15 21:00:56 GMT 2013&datum=02/15/2013&zauzetost=ZAUZET }
  • 我的服务崩溃了...我已经在活动中成功使用过几次了不知道现在是什么问题。也许我应该更改并用一些特殊字母登录链接(这不是使用 URL 方法时需要)。
【解决方案2】:

我得到它的工作与此

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

【讨论】:

  • 我注意到的是网站链接字符串的大小是造成问题的大小!
  • 主要问题是我的链接中有一些空格符号,删除它们后它可以正常工作!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-25
相关资源
最近更新 更多