如何在Android中使用Json从Mysql中检索mobilenumber

时间:2016-11-30 03:39:36

标签: android json android-volley

我正在使用JSON在用户登录时从MySQL数据库中检索详细信息(fname,email,bikeno,mobileno)。除移动电话号码外,所有细节都在检索。

My DateBase Table

Thats the phone number

我的代码是:

progressBar = new ProgressDialog(v.getContext());
progressBar.setCancelable(true);
progressBar.setMessage("Logging You in");
progressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressBar.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, login_url,
    new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONArray jsonArray = new JSONArray(response);
                JSONObject jsonObject = jsonArray.getJSONObject(0);
                JSONObject jsonObject1= jsonArray.getJSONObject(0);
                String code = jsonObject.getString("code");

                if (code.equals("login_failed")) {

                    builder.setTitle("Login Error");
                    displayAlert(jsonObject.getString("message"));
                } else {

                    Intent intent = new Intent(MainActivity.this, Home.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("email1", jsonObject.getString("email"));
                    bundle.putString("bikeno1", jsonObject.getString("bikeno"));
                    bundle.putString("fname", jsonObject.getString("fname"));
                    bundle.putString("mobileno", jsonObject.getString("mobileno"));
                    intent.putExtras(bundle);

                    startActivity(intent);
                    finish();
                }
            } catch(Exception e){
            }
        }
    }
});

我的问题是如何获取手机号码以及如何将该值发送到其他活动?

1 个答案:

答案 0 :(得分:2)

检查一下: PHP:

<?php 

$sql = "SELECT * FROM tablename";

require_once('connectionphp.php');

$r = mysqli_query($conn,$sql);

$result1 = array();

while($row = mysqli_fetch_array($r)){
array_push($result1,array(
    'mobilenum'=>$row['mobilenum']
));
}

echo json_encode($result1);

并尝试在你的java中检索它:

  StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Toast.makeText(Activity.this,response,Toast.LENGTH_LONG).show();


                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(Activity.this,error.toString(),Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String , String>();
            params.put(KEY_MOBILE, mobilenum);


            return params;
        }

    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);
}

并在下一个Activity中获取这样的包:

 Bundle extras = getIntent().getExtras();
    if(extras != null){
        mob= extras.getString("mobileno");
    }