android volley发布json ID并从PHP服务器获取结果 [英] android volley post json ID and get result back from PHP server

查看:118
本文介绍了android volley发布json ID并从PHP服务器获取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使之工作,基本上我是从以前的活动中使用intent来获得一个ID的,现在我想将此ID发送到服务器,以便它返回与此ID关联的所有数据.

i am struggling to make this work, basically i get an id from previous activity using intent, now i want to send this id to server so it returns all the data associated with this id.

javacode

final String URL = "URL";
// Post params to be sent to the server
HashMap<String, String> params = new HashMap<String, String>();
params.put("ID", "1");

JsonObjectRequest req = new JsonObjectRequest(URL, new JSONObject(params),
       new Response.Listener<JSONObject>() {
           @Override
           public void onResponse(JSONObject response) {
               try {
                   VolleyLog.v("Response:%n %s", response.toString(4));
                    print response in textview;
               } catch (JSONException e) {
                   e.printStackTrace();
               }
           }
       }, new Response.ErrorListener() {
           @Override
           public void onErrorResponse(VolleyError error) {
               VolleyLog.e("Error: ", error.getMessage());
           }
       });

// add the request object to the queue to be executed
ApplicationController.getInstance().addToRequestQueue(req);

PHP服务器

if (!empty($_POST)) {

    $query = " SELECT * FROM table WHERE ID = :ID " ;

    $query_params = array(
        ':ID' => $_POST['ID'],);

    try {
        $statement   = $db->prepare($query);
        $result = $statement->execute($query_params);
    }
    catch (PDOException $ex) {


        $response["success"] = 0;
        $response["message"] = "Database query error";
        die(json_encode($response));

    }

    $result = $statement->fetchAll();

    if($result){

            $data =array();

            foreach($result as $rows){
        $json = array();
        $json["Name"] = $rows["Name"];





        array_push ($data,$json);



            }
     echo stripcslashes(json_encode($data, JSON_PRETTY_PRINT));

推荐答案

我使用了

i used this example of http://www.itsalif.info/content/android-volley-tutorial-http-get-post-put and coverted the string response to string array, worked for me

url = "http://httpbin.org/post";
    StringRequest postRequest = new StringRequest(Request.Method.POST, url, 
        new Response.Listener<String>() 
        {
            @Override
            public void onResponse(String response) {
                // response
                Log.d("Response", response);
            }
        }, 
        new Response.ErrorListener() 
        {
             @Override
             public void onErrorResponse(VolleyError error) {
                 // error
                 Log.d("Error.Response", response);
           }
        }
    ) {     
        @Override
        protected Map<String, String> getParams() 
        {  
                Map<String, String>  params = new HashMap<String, String>();  
                params.put("name", "Alif");  
                params.put("domain", "http://itsalif.info");

                return params;  
        }
    };
    queue.add(postRequest);

这篇关于android volley发布json ID并从PHP服务器获取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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