应用程序将整数发送到MySQL服务器,但拒绝发送字符串

时间:2018-03-04 20:30:43

标签: android mysql android-studio

我正在创建一个应用程序,它应该发送一个用户名和他有多少硬币到MySQL服务器,但是当应用程序运行它没有提交数据到服务器,我试图改变vars发送到一个Int和另一个Int而不是一个字符串和一个Int,它工作,所以应用程序只有当vars为Int时才成功地将数据提交给服务器,任何想法我做错了什么? 我的代码:

public void pushToServer(){
    String url = "http://192.168.1.103/push.php";

    StringRequest sq = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            protected Map<String, String > getParams(){
                Map<String, String> parr = new HashMap<String, String>();


                int my = myBalance;
                String me = "Matthews";
                parr.put("username", String.valueOf(me));
                parr.put("coin", String.valueOf(my));

                return parr;

            }

        };
    AppController.getInstance().addToRequestQueue(sq);
    Toast.makeText(getApplicationContext(), "Pushed to server!", Toast.LENGTH_LONG).show();
}

AppController.java:

public class AppController extends Application {

public static final String TAG = AppController.class.getSimpleName();

private RequestQueue mRequestQueue;

private static AppController mInstance;

@Override
public void onCreate() {
    super.onCreate();
    mInstance = this;
}

public static synchronized AppController getInstance() {
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }

    return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> req, String tag) {
    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    getRequestQueue().add(req);
}

public <T> void addToRequestQueue(Request<T> req) {
    req.setTag(TAG);
    getRequestQueue().add(req);
}

public void cancelPendingRequests(Object tag) {
    if (mRequestQueue != null) {
        mRequestQueue.cancelAll(tag);
    }
}
}

我的PHP文件:

<?php

//open connection to mysql db
$connection = mysqli_connect("localhost","root","","app")
or die("Error " . mysqli_error($connection));

// recieved from app
$username = $_POST['username'];
$coin = $_POST['coin'];

echo "Response: ".$username." ";

$sql_query = "INSERT INTO users (username,coin) VALUES 
('$username','$coin')";

if (mysqli_query($connection, $sql_query)) {
echo "Posted to server";
} else {
echo "Error: " . $sql_query . "<br>" . mysqli_error($connection);
}

mysqli_close($conn);

?>

正如我所说,将String用户名更改为Int可以正常工作,但如果它是一个字符串则无法正常工作,我完全迷失了。有什么想法吗?

0 个答案:

没有答案