Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用Android Studio Volley库将JSON数组发送到HTTP请求_Php_Arrays_Json_Android Studio_Http - Fatal编程技术网

Php 如何使用Android Studio Volley库将JSON数组发送到HTTP请求

Php 如何使用Android Studio Volley库将JSON数组发送到HTTP请求,php,arrays,json,android-studio,http,Php,Arrays,Json,Android Studio,Http,我做了很多研究,但没有成功 我有一个android应用程序。在这里,我可以将保存人员名称添加到SQLite数据库 我想要一个SYNC按钮,然后使用restapi(HTTP请求)将数据推送到MYSQL中 因此,首先,我创建了一个函数来从names表中获取所有数据 //get the data to to send to mysql final Cursor data = mDatabaseHelper.getData(); //Create an Array li

我做了很多研究,但没有成功

我有一个android应用程序。在这里,我可以将保存人员名称添加到SQLite数据库

我想要一个SYNC按钮,然后使用restapi(HTTP请求)将数据推送到MYSQL中

因此,首先,我创建了一个函数来从names表中获取所有数据

        //get the data to to send to mysql
    final Cursor data = mDatabaseHelper.getData();

    //Create an Array list
    final ArrayList<String> listData = new ArrayList<>();


    //Declare json array
    final JSONArray datatoperse = new JSONArray();


    //Store the result in array using while loop
    while (data.moveToNext()){
        //get the value from the database in column 1
        //than add to array list
        listData.add(data.getString(1));

        //append data to json array
        datatoperse.put(data.getString(1));

    }
    System.out.println(datatoperse.toString());
然后将数据保存到JSON数组中。 接下来,我想向服务器发送HTTP请求

  String url = "server host name";

        JsonArrayRequest request = new JsonArrayRequest(Request.Method.POST, url, null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {

                final String result = response.toString();
                Log.d("Response","result :" + result);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        })
        {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> params = new HashMap<String,String>();

                   params.put("array",jsonText);


                return super.getParams();
            }
        }  ;
        mQueue.add(request);
String url=“服务器主机名”;
JsonArrayRequest=newJSONArrayRequest(request.Method.POST,url,null,new Response.Listener()){
@凌驾
公共void onResponse(JSONArray响应){
最终字符串结果=response.toString();
日志d(“响应”,“结果:+结果”);
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
错误。printStackTrace();
}
})
{
@凌驾
受保护的映射getParams()引发AuthFailureError{
Map params=新的HashMap();
参数put(“数组”,jsonText);
返回super.getParams();
}
}  ;
添加(请求);
我有一个php页面,该请求将发送到该页面

<?php

 //Receive the RAW post data.
$content = $_POST["name"];
//$json = '{"name": "wewqewq"}';
$obj = json_decode($content);


 $insert_stmt = $mysqli_scs->prepare("INSERT INTO xamarinuserregister_ur (name) VALUES (?)");
 $name =$obj->{'name'};


 $insert_stmt->bind_param("s", $name);
 //Execute the statement
 $insert_stmt->execute();


?>
试试这段代码

<?php

// Receive the RAW post data.
$content = file_get_contents("php://input");
// $json = '{"name": "wewqewq"}';
$obj = json_decode($content);

$insert_stmt = $mysqli_scs->prepare("INSERT INTO xamarinuserregister_ur (name) VALUES (?)");
$name = $obj->name;

$insert_stmt->bind_param("s", $name);
// Execute the statement
$insert_stmt->execute();

?>
您可以使用我的库

这是你的解决方案

// Init library YOU CAN WRITE IT ONE TIME
GridmiAPI.init("http://example.com/API/", 8000, JSONObject.class);

// Create request
GridmiAPI.Request request = new GridmiAPI.Request("POST", "load");

// TODO FULL URL EQUAL `STATIC + ENDPOINT` = "http://example.com/API/load"

// Create body JSONArray of the request
JSONArray jsonArrayBody = new JSONArray();
jsonArrayBody.put("Item 1");
jsonArrayBody.put("Item 2");
jsonArrayBody.put("Item 2");

// Add to request
request.setBody(jsonArrayBody);

// Send request
GridmiAPI.onRequest(this, request, new GridmiAPI.Handler.OUT() {

    @Override
    protected void onSuccess(GridmiAPI.Response response) {
        try {
            boolean result = ((JSONObject) response.getData()).getBoolean("result");
            ((TextView) findViewById(R.id.title)).setText(result ? "OK" : "NOT OK");
        } catch (Exception exception) {
            this.onFailed(exception);
        }
    }

    @Override
    protected void onFailed(Exception exception) {
        Toast.makeText(MainActivity.this, exception.getMessage(), Toast.LENGTH_LONG).show();
    }

}).start();

我得到错误:W/System.err:com.android.volley.ParseError:org.json.JSONException:Value
// Init library YOU CAN WRITE IT ONE TIME
GridmiAPI.init("http://example.com/API/", 8000, JSONObject.class);

// Create request
GridmiAPI.Request request = new GridmiAPI.Request("POST", "load");

// TODO FULL URL EQUAL `STATIC + ENDPOINT` = "http://example.com/API/load"

// Create body JSONArray of the request
JSONArray jsonArrayBody = new JSONArray();
jsonArrayBody.put("Item 1");
jsonArrayBody.put("Item 2");
jsonArrayBody.put("Item 2");

// Add to request
request.setBody(jsonArrayBody);

// Send request
GridmiAPI.onRequest(this, request, new GridmiAPI.Handler.OUT() {

    @Override
    protected void onSuccess(GridmiAPI.Response response) {
        try {
            boolean result = ((JSONObject) response.getData()).getBoolean("result");
            ((TextView) findViewById(R.id.title)).setText(result ? "OK" : "NOT OK");
        } catch (Exception exception) {
            this.onFailed(exception);
        }
    }

    @Override
    protected void onFailed(Exception exception) {
        Toast.makeText(MainActivity.this, exception.getMessage(), Toast.LENGTH_LONG).show();
    }

}).start();