Android:未调用Volley String请求响应

时间:2016-05-14 14:05:42

标签: java php android android-volley

我正在尝试使用POST方法将数据从android发送到php

php部分单独测试,它100%正常工作,它返回一个假设的json字符串

android部分无效 它不会打印错误 甚至是必要的回应

这是代码并经过测试,使用Toast打印字符串“test”,但它在凌空代码中没有变化:(

这是php代码:

<?php
    require_once(__DIR__.'/../dbConnect.php');  


    $userName=$_POST['username']; 

    $query = "SELECT * FROM users WHERE username='$userName'";
    $result = pg_query($con, $query);
    $rows = pg_num_rows($result);

    while ($row = pg_fetch_row($result)) 
    {
        $field = pg_num_fields( $result );

        for ( $i = 0; $i < $field; $i++ ) {

            $name = pg_field_name( $result, $i );
            $user_arr[$name] = $row[$i]; 
        }               
    }

    echo json_encode($user_arr);

    pg_close($con); 

    ?>

这是android代码:

初始化测试字符串以显示输出

String test = "none";

这是我称之为方法的地方

  setUserData(username);
  Toast.makeText(LoginActivity.this, test, Toast.LENGTH_SHORT).show();

这是方法:

  private void setUserData(final String username)
    {
        StringRequest strReq = new StringRequest(Request.Method.POST,
                Server.GET_USERDATA_URL, new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

                   test= "test = "+response;

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(LoginActivity.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();

            }
        })

        {

            @Override
            protected Map<String, String> getParams() {
                // Posting parameters to getData url
                Map<String, String> params = new HashMap<String, String>();
                params.put(Server.KEY_USERNAME, username);
                return params;
            }

        };
        //Creating a Request Queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        //Adding request to the queue
        requestQueue.add(strReq);
    }

2 个答案:

答案 0 :(得分:1)

检查您是否已获得互联网许可。

还在

中添加吐司
onResponse()
 Toast.makeText(LoginActivity.this, response.toString(), Toast.LENGTH_LONG).show(); 

答案 1 :(得分:1)

您遇到并发问题。当您执行setUserData(username)方法时,Volley正在后台线程上完成工作。因此,您需要等待响应才能在Toast中显示它。在你打电话的那一刻:

Toast.makeText(LoginActivity.this, test, Toast.LENGTH_SHORT).show();

服务器仍然没有回复,test仍然是none。您需要从onResponse回调中调用toast:

private void setUserData(final String username) {

    StringRequest strReq = new StringRequest(
        Request.Method.POST,
        Server.GET_USERDATA_URL, 
        new Response.Listener<String>() {

            @Override
            public void onResponse(String response) {

                test = "test = "+response;
                showToast();

            }
        }, 
        new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(LoginActivity.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();

        }
    })

    {

        @Override
        protected Map<String, String> getParams() {
            // Posting parameters to getData url
            Map<String, String> params = new HashMap<String, String>();
            params.put(Server.KEY_USERNAME, username);
            return params;
        }

    };
    //Creating a Request Queue
    RequestQueue requestQueue = Volley.newRequestQueue(this);
    //Adding request to the queue
    requestQueue.add(strReq);
}

private void showToast() {

    Toast.makeText(LoginActivity.this, test, Toast.LENGTH_SHORT).show();
}