Android Volley 参数

标签 android android-volley

在我的项目中,我在 JSONobject 请求的参数中传递了用户的名字。然后它会得到响应并填充 TextView 。但是我无法弄清楚为什么我的代码不起作用。我检查了我的 php,当我在其中放入预定义的名字时它工作正常,所以我排除了 Web 服务问题。它是先得到响应然后传递参数吗?请帮忙

public class ProfileActivity extends AppCompatActivity {

TextView Username, Firstname, Lastname, Birthdate, Barangay;

String firstname;
String json_url = "http://localhost/android/getprofileinfo.php";



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);

    Username = (TextView)findViewById(R.id.usernameprofile);
    Firstname = (TextView)findViewById(R.id.firstnameprofile);
    Lastname = (TextView)findViewById(R.id.lastnameprofile);
    Birthdate = (TextView)findViewById(R.id.birthdayprofile);
    Barangay = (TextView)findViewById(R.id.barangayprofile);

   final Bundle bundle = getIntent().getExtras();

    firstname = bundle.getString(firstname);





    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, json_url, (String) null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        Username.setText(response.getString("username"));
                        Firstname.setText(response.getString("firstname"));
                        Lastname.setText(response.getString("lastname"));
                        Birthdate.setText(response.getString("birthdate"));
                        Barangay.setText(response.getString("barangay"));
                    }

                    catch(JSONException e){
                            e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            Toast.makeText(ProfileActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
            error.printStackTrace();
        } //end of method onErrorResponse

    })
    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("firstname", firstname);
            return params;
        }
    };
    MySingleton.getmInstance(ProfileActivity.this).addTorequestque(jsonObjectRequest);

}

这是 getpropileinfo.php

<?php

$firstname =$_POST["firstname"];


define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','mydb');

$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');

$sql = "SELECT username,firstname,lastname,birthdate,barangay FROM users 
WHERE firstname LIKE '".$firstname."'; ";

$result = mysqli_query($con, $sql);

if(mysqli_num_rows($result) > 0)
{
$row = mysqli_fetch_assoc($result);
echo json_encode(array("username"=>$row['username'], 
"firstname"=>$row['firstname'], 
    "lastname"=>$row['lastname'], "birthdate"=>$row['birthdate'], 
"barangay"=>$row['barangay']));
}
?>

最佳答案

您没有正确获取 getExtras(),请提供您在调用 Activity 中使用 putExtra() 传递的 key 名称,如下所示

在调用 Activity 中传递 Intent ,如

Intent i = new Intent(FirstActivity.this, ProfileActivity.class);   
String strName = "some_name";
i.putExtra("key_username", strName);
startActivity(i);

然后在ProfileActivity中,

final Bundle bundle = getIntent().getExtras();
firstname = bundle.getString("key_username");

转换为JsonArrayRequest

JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, json_url, (String) null,
            new com.android.volley.Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    try {

                            JSONObject person = (JSONObject) response
                                    .get(0);

                            Username.setText(person.getString("username"));
                            Firstname.setText(person.getString("firstname"));
                            Lastname.setText(person.getString("lastname"));
                            Birthdate.setText(person.getString("birthdate"));
                            Barangay.setText(person.getString("barangay"));



                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(getApplicationContext(),
                                "Error: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }

                }
            }, new com.android.volley.Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            Toast.makeText(ProfileActivity.this, "Something went wrong", Toast.LENGTH_SHORT).show();
            error.printStackTrace();
        } //end of method onErrorResponse

    })
    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("firstname", firstname);
            return params;
        }
    };

关于Android Volley 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49953261/

相关文章:

java - ActionBarDrawerToggle 不接受 R.drawable 文件

android - 无法覆盖 Kotlin 中的 getParam (Volley)

java - Volley JSONException : End of input at character 0 of

php - Volley Json 请求不起作用 - 字符串无法转换为 JsonObject/JsonArray

java - 数学公式的表示

android - 为什么我的白噪声有跳动/爆音/咩咩声?

java - db4o获取特定对象android java

android - 如何适应圆形imageView?

sockets - “Volley”错误: java.net.SocketException:recvfrom失败:ECONNRESET(对等连接重置)

android - 对 Volley Android 网络库的 HTTPS 支持