aenugula karthik aenugula karthik - 3 days ago 5
Android Question

how to retrieve mobilenumber from Mysql using Json in Android

I am Using

JSON
to retrieve details(fname,email,bikeno,mobileno) from MySQL database when user login. All details are retreiving except mobile number.

My DateBase Table

Thats the phone number

My Code is :

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){
}
}
}
});


My Question is how to get mobile number and how to send that value to another activity?

Answer

Check this out: 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);

and try to retrive it in your java like this:

  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);
}

and get the bundle like this in next Activity:

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