2016-11-30 25 views
0

我使用JSON从用户登录时从MySQL数据库检索详细信息(fname,email,bikeno,mobileno)。除手机号码外,所有详细信息都将被删除。如何使用Json在Android中从Mysql检索移动号码

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

我的问题是如何获得的手机号码,以及如何发送值到另一个活动?

+0

是你的移动没有​​在你的JSON响应。 –

+0

否bro。它显示记录你,但它不记录当我评论这行“bundle.putString(”mobileno“,jsonObject.getString(”mobileno“));”那么它能够登录到应用程序 –

+1

在'response'中显示'JSON'。 –

回答

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

,并获得捆绑像这样的下一个活动:

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