无法将Java变量导入PHP

时间:2014-01-02 20:19:44

标签: java php android variables

我正在尝试将变量从Java传递给PHP,但无论我做什么,它都无法正常工作。 我甚至不明白我如何在PHP中看到Java变量... 我很困惑,不知道接下来该做什么。

我当前的java代码看起来像这样;

    public void mahGPSbutton() {


    Button myGPS_button = (Button) findViewById(R.id.GPS_button);
    myGPS_button.setTextColor(Color.BLACK);
    myGPS_button.setOnClickListener(new View.OnClickListener() {
                                                                                                                              //#
        public void onClick(View v) {   
            LocationManager locationManager = (LocationManager) MainActivity.this.getSystemService(Context.LOCATION_SERVICE); //#
            LocationListener locationListener = new LocationListener() {                                                      //#
                public void onLocationChanged(Location location, String url) {
                    double MyLat = location.getLatitude();
                    double MyLong = location.getLongitude();
                    double MyAlt = location.getAltitude();
                    String MyProvider = location.getProvider();
                    float MyAccuracy = location.getAccuracy();
                    Object URI = "http://192.168.1.41/School/Binder.php?MyLat="+MyLat;
                    TextView TV = (TextView) findViewById(R.id.gPstextView1);
                    TV.setText("Your current location is: " + MyLat + "\n" + MyLong + "\n" + MyAlt + "\n" + MyProvider + "\n" + MyAccuracy);

                    try
                    {
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost("http://192.168.1.41/School/Binder.php?myLat="+ MyLat);
                        httppost.setURI((java.net.URI) URI);
                        HttpResponse response = httpclient.execute(httppost);
                        HttpEntity entity = response.getEntity();
                        InputStream is = entity.getContent();
                    }catch(Exception e){
                        Log.e("log_tag", "Error in http connection"+e.toString());
                        }
                }
                public void onStatusChanged(String provider, int status, Bundle extras) {}                                    //#
                public void onProviderEnabled(String provider) {}                                                             //#
                public void onProviderDisabled(String provider) {}
                //#
                @Override
                public void onLocationChanged(Location arg0) {
                    // TODO Auto-generated method stub

                }
            };                                                                                                                //#
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
                                                                                                              //#
        }                                                                                                                     //#
    }); 
}                                                                                                                             //#

和服务器端的php文件:

<?php
$LAT_VARIABLE = $_GET['MyLat'];
$LONG_VARIABLE = $_GET['MyLong'];
$ALT_VARIABLE = $_GET['MyAlt'];
$PRO_VARIABLE = $_GET['MyProvider'];
$ACC_VARIABLE = $_GET['MyAccuracy'];

echo "Number 1: ".$LAT_VARIABLE."";
echo "Can you see the value?";

?>
顺便说一句,我试图用这种方式

String URL = "http://192.168.1.41/School/Binder.php?MyLat="+MyLat;

在楼下的Try / catch地方

httppost.setURI(URL);

但是最后一行给了我一个错误,说改了URL到URI但是然后用String URL = ....给我另一个错误,说要把它改成字符串...... 所以我可以帮助我,或者给我一些想法,我应该怎么做? 以及我应该如何看待变量的值?只需输入http://192.168.1.41/School/Binder.php吧?

顺便说一下,我在本地做这一切,服务器和Android设备都在同一个网络上。

感谢您的建议...... 此致

2 个答案:

答案 0 :(得分:2)

将您的PHP代码更改为:

<?php
$LAT_VARIABLE = $_REQUEST['MyLat'];
$LONG_VARIABLE = $_REQUEST['MyLong'];
$ALT_VARIABLE = $_REQUEST['MyAlt'];
$PRO_VARIABLE = $_REQUEST['MyProvider'];
$ACC_VARIABLE = $_REQUEST['MyAccuracy'];

echo "Number 1: ".$LAT_VARIABLE."";
echo "Can you see the value?";

error_log("Lat: " . $LAT_VARIABLE, 0);

?>

但这不是主要问题。我稍微修改了一下你的代码,得到了:

android.os.NetworkOnMainThreadException

这是Android中的经典问题。您需要在AsyncTaskThread中运行网络代码。

我通过这样的AsyncTask来解决这个问题:

    locationManager = (LocationManager) MainActivity.this
            .getSystemService(Context.LOCATION_SERVICE); // #

    clickableButtonWithBorder.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

                    locationListener = new LocationListener() {

                        @Override
                        public void onStatusChanged(String provider,
                                int status, Bundle extras) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void onProviderEnabled(String provider) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void onProviderDisabled(String provider) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void onLocationChanged(Location location) {

                            double MyLat = location.getLatitude();
                            double MyLong = location.getLongitude();
                            double MyAlt = location.getAltitude();
                            String MyProvider = location.getProvider();
                            float MyAccuracy = location.getAccuracy();
                            Object URI = "http://localhost/Binder.php?MyLat="
                                    + MyLat;
                            Toast.makeText(
                                    getApplicationContext(),
                                    "Your current location is: " + MyLat
                                            + "\n" + MyLong + "\n" + MyAlt
                                            + "\n" + MyProvider + "\n"
                                            + MyAccuracy,
                                    Toast.LENGTH_SHORT).show();

                            NetworkOperation op = new NetworkOperation(MyLat);
                            op.execute("");

                        };
                    }; // #

                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER, 0, 0, locationListener);

            // #
        }                                                                                                                     //#
}); 
    }      

    private class NetworkOperation extends AsyncTask<String, Void, String> {

    double MyLat;

    public NetworkOperation(double lat)
    {
        MyLat = lat;
    }


    @Override
    protected String doInBackground(String... params) {
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://10.33.146.21/Binder.php?MyLat="
                            + MyLat);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            InputStream is = entity.getContent();

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection" + e.toString());
        }
        return "Executed";
    }

}

最后,我注意到你在某些地方使用了“MyLat”而在其他地方使用了“myLat”。让他们全部“MyLat”。它适用于我:

[Thu Jan 02 16:03:01 2014] [error] [client 10.35.134.163] Lat: 33.9305431

答案 1 :(得分:1)

看起来你正在用Java进行POST,但试图用PHP进行GET。