php - 使用volley php发布数据

标签 php android mysql android-volley

我想使用volley来发布数据。

这是我的 php 脚本:

<?php header('Content-Type: application/json; charset=utf-8');

/*
 * Following code will update a product information
 * A product is identified by product id (pid)
 */

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['quotes_id']) && isset($_POST['quote']) && isset($_POST['uid']) && isset($_POST['imageName']) && isset($_POST['nameOfCreator']) ) {

    $quotes_id = $_POST['quotes_id'];
    $quote = $_POST['quote'];
    $uid = $_POST['uid'];
    $imageName = $_POST['imageName'];
    $nameOfCreator = $_POST['nameOfCreator'];

    // include db connect class
    require_once __DIR__ . '/db_connect.php';

    // connecting to db
    $db = new DB_CONNECT();

    // mysql update row with matched pid
    $result = mysql_query("INSERT INTO quotes SET quote = '$quote', uid = '$uid',  imageName = '$imageName', nameOfCreator = '$nameOfCreator'");

    // check if row inserted or not
    if ($result) {
        // successfully updated
        $response["success"] = 1;
        $response["message"] = "Product successfully updated.";

        // echoing JSON response
        echo json_encode($response);
    } else {

    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

以下是我在单击按钮时调用的方法:

private void setData() {
        Map<String, String> jsonParams = new HashMap<String, String>();
        jsonParams.put(StaticVariables.QUOTES_ID, null);
        jsonParams.put(StaticVariables.QUOTE, "ööö");
        jsonParams.put(StaticVariables.UID, "112");
        jsonParams.put(StaticVariables.IMAGE_NAME, "112v1.jpg");
        jsonParams.put("nameOfCreator", "aa");

        JsonObjectRequest myRequest = new JsonObjectRequest(
                Request.Method.POST,
                StaticVariables.url_insert_quote,
                new JSONObject(jsonParams),

                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
//                        verificationSuccess(response);
                        Log.e("JA", "nice");
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
//                        verificationFailed(error);
                    }
                }) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                headers.put("Content-Type", "application/json; charset=utf-8");
                headers.put("User-agent", "My useragent");
                return headers;
            }
        };
        AppController.getInstance().addToRequestQueue(myRequest, "tag");
    }

但是我的 mysql 数据库中不会插入任何行。 此外,我还设置了互联网权限以及互联网连接所需的一切。

最佳答案

尝试这个代码,它在 Android 应用程序中对我有用,并且您必须使用另一个网址来发布:

private void registerUser(){
        final String username = editTextUsername.getText().toString().trim();
        final String password = editTextPassword.getText().toString().trim();


        StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        //do stuffs with response of post
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        //do stuffs with response erroe
                    }
                }){
            @Override
            protected Map<String,String> getParams(){
                Map<String,String> params = new HashMap<String, String>();

                params.put(KEY_PASSWORD,password);
                params.put(KEY_EMAIL, email);
                return params;
            }

        };

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }

我不确定你的 php 代码,但上面的 android 部分可以工作,

你可能知道你必须这样做,

添加互联网权限:

<uses-permission android:name="android.permission.INTERNET" /> 

将volley添加到build.gradle:

compile 'com.mcxiaoke.volley:library:1.0.19'

谢谢

关于php - 使用volley php发布数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34072845/

相关文章:

PHPunit 为所有测试套件提供不同的 Bootstrap

Javascript 按钮在表单标签中不起作用

php - 如果从数据库中选择的值为 1,如何阻止用户?

php - 在 PHP Web 应用程序中使用时区

php - 我是将常量放入数据库还是使用文件来保存它

php - 从数据库获取数据无法正常工作

java - 致命异常 : java. lang.OutOfMemoryError : pthread_create at com. google.maps.api

android - 如何在 4.2 之前的 Android 版本上处理 RTL 语言?

javascript - 我在react-native中获取了带有fetch的JSON,如何将结果发送到其他组件

Mysql长子查询 "in"问题