当我从android studio运行我的php代码时,为什么没有POST数据?

时间:2018-07-22 01:01:00

标签: java php android post type-mismatch

我正在使用POST方法将数据输入到我的php文档中,然后再输入到我的mysql数据库中,但是当我运行我的php代码时,它说没有POST数据,并且在我的android studio日志中显示JSONObject类型不匹配异常。创建JSONObject时发生异常。奇怪的是,我在项目的其他部分使用相同的代码,但使用了不同的变量,没有问题。没有列插入到我的mysql表。感谢您的帮助。

这是我的Java代码-

private void updateCoords(){
    String latitude = Double.toString(currentLocation.getLatitude());
    String longitude = Double.toString(currentLocation.getLongitude());
    String tableName = "queue_" + Integer.toString(rider.getFrat_code());
    Response.Listener<String> responseListener = new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.i(TAG, "updateCoords Response Listener: " + response);
            try {
                //Log.i(TAG, "["+result+"]");
                JSONObject jsonResponse = new JSONObject(response);
                boolean success = jsonResponse.getBoolean("success"); //found in CoordsPush.php file

                if (success){
                    Log.i(TAG, "updateCoords: " + response);
                } else {
                    AlertDialog.Builder builder = new AlertDialog.Builder(MapsActivity.this);
                    builder.setMessage("Upload coordinates failed")
                            .setNegativeButton("Retry", null)
                            .create().show();
                    Log.d(TAG, "Failed to upload coordinates");
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d(TAG, "Errorlistener: " + error.toString());
        }
    };

    PushCoordsRequest pushCoordsRequest = new PushCoordsRequest(Integer.toString(rider.getID()),
            latitude, longitude, tableName, responseListener, errorListener);
    RequestQueue requestQueue = Volley.newRequestQueue(MapsActivity.this);
    requestQueue.add(pushCoordsRequest);
}

这是pushcoordsrequest类:

import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.HashMap;
import java.util.Map;

public class PushCoordsRequest extends StringRequest{

private static final String coordinatePushURL = "myurl";
private Map<String, String> params;

private final String TAG = "PushCoords Request: ";

public PushCoordsRequest (String id, String latitude, String longitude, String tableName,
                          Response.Listener<String> listener, 
Response.ErrorListener errorListener){
    super(Method.POST, coordinatePushURL, listener, errorListener);
    params = new HashMap<String, String>();
    params.put("ID", id);
    params.put("Latitude", latitude);
    params.put("Longitude", longitude);
    params.put("TableName", tableName);
}

@Override
public Map<String, String> getParams() {
    return params;
}
}

这是我的php:

<?php
$con = mysqli_connect("localhost", "username", "password", "id6042396_users");

if (empty($_POST))
{
    echo "no post data";
}
$id = $_POST["ID"];
$lat = $_POST["Latitude"];
$long = $_POST["Longitude"];
$table = $_POST["TableName"];
$statement = mysqli_prepare($con, "INSERT INTO ? (ID, Latitude, Longitude) VALUES (?, ?, ?)");
echo $statement;
mysqli_stmt_bind_param($statement, "sidd", $table, $id, $lat, $long);
mysqli_stmt_execute($statement);

$response = array();
$response["success"] = true;  

echo json_encode($response);
?>

0 个答案:

没有答案