使用PHP的凌空数据后 [英] Post data using volley php

查看:211
本文介绍了使用PHP的凌空数据后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用凌空发布的数据。

I am would like to use volley to post data.

这是我的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);
}
?>

和下面是我的方法,我叫上点击一个按钮:

and the following is my method which I called on a button click:

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数据库插入..
此外,我已经设置Internet权限和一切是必要的网络连接。

But no row will be inserted in my mysql database.. Additionally, I have set INTERNET PERMISSION and everything that is necessary for a internet connection.

推荐答案

试试这个code它的Andr​​oid应用工作对我来说,你必须使用另一个URL发布:

Try this code it worked for me in android app, and you have to use another url to post :

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 code,但上述Android部分作品,

I'm not sure with you php code, but above android part works,

您可能知道,你必须,

添加网络权限:

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

添加抽射的build.gradle:

Add volley to build.gradle:

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

感谢

这篇关于使用PHP的凌空数据后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆