Not getting value from server into my Textview usi

2020-04-20 06:36发布

I am new to android , here if i choose forgot password link it should go to next activity and there if i write correct email then it should setText the correct password from server.But i am not getting value from server . here is my ForgotPassword.java:

public class ForgotPasswordActivity extends AppCompatActivity {

 private String fremail;
 private ProgressDialog pDialog;
 protected EditText femail;
 protected Button mSubmitButton;
 TextView pas;

 private static String url_create_book = "http://cloud......com/broccoli/fpassword.php";


 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_forgot_password);
  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  setSupportActionBar(toolbar);
  getSupportActionBar().setDisplayHomeAsUpEnabled(true);

  pas = (TextView) findViewById(R.id.pas);
  femail = (EditText) findViewById(R.id.feml);

  Button submit = (Button) findViewById(R.id.sbtn);
  submit.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {

    pDialog = new ProgressDialog(ForgotPasswordActivity.this);
    pDialog.setMessage("Please wait..");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(true);
    pDialog.show();
    fremail = femail.getText().toString();

    // new CreateNewProduct().execute();
    StringRequest stringRequest = new StringRequest(Request.Method.POST, url_create_book,
     new Response.Listener < String > () {
      @Override
      public void onResponse(String response) {
       pDialog.dismiss();

       try {
        JSONObject object = new JSONObject(response);
        JSONArray jsonArray = object.getJSONArray("result");
        JSONObject jsonObject = jsonArray.getJSONObject(0);

        // fetch password from JSON
        String password = jsonObject.getString("password");
        // use password in textviewfemail.setText(password, TextView.BufferType.EDITABLE);
        pas.setText(password, TextView.BufferType.EDITABLE);
       } catch (JSONException e) {
        Toast.makeText(ForgotPasswordActivity.this, e.toString(), Toast.LENGTH_LONG).show();
       }


      }

     },

     new Response.ErrorListener() {
      @Override
      public void onErrorResponse(VolleyError error) {
       pDialog.dismiss();
       Toast.makeText(ForgotPasswordActivity.this, error.toString(), Toast.LENGTH_LONG).show();
      }
     }) {
     @Override
     protected Map < String, String > getParams() {
      Map < String, String > params = new HashMap < String, String > ();
      params.put("email", fremail);


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

   }

  });
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
   case android.R.id.home:
    onBackPressed();
    return true;
   default:
    return super.onOptionsItemSelected(item);
  }
 }

}

here is my php code:

<?php 

    include ('config.php');

    // updated here, value is mapped 

     $email = $_POST['email'];

    //$email = 'aliza@gmail.com';

    $sql = "SELECT * FROM UserInfo WHERE email='".$email."'";
    $r = mysqli_query($conn,$sql);

    $num = mysqli_num_rows($r);

    if($num == 0){
    echo "Invalid email";
    }
    else{

    $result = mysqli_fetch_array($r);

    }

    $data['password'] = $result['password'];  

    echo json_encode($data);   
    mysqli_close($conn);

?>

I am not getting error in logcat i am just getting this in mobile jsonException. Index 0 out of 0 range[0...0]

here is my json response:

{
    "password": ""
}

1条回答
小情绪 Triste *
2楼-- · 2020-04-20 07:04

i got the answer...my php code is fine which i posted above the problem is with json object and json array as @Pier Giorgio Misley told me .so here is my updates activity:

 StringRequest stringRequest = new StringRequest(Request.Method.POST, url_create_book,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                pDialog.dismiss();

                                try {
                                    JSONObject object     = new JSONObject(response);

                                 //   JSONArray jsonArray   = object.getJSONArray(0);
                                  //  JSONObject jsonObject = jsonArray.getJSONObject(0);

// fetch password from JSON
                                    String password         = object.getString("password");
// use password in textviewfemail.setText(password, TextView.BufferType.EDITABLE);
                                    pas.setText(password, TextView.BufferType.EDITABLE);



                                }
                                catch (JSONException e) {
                                    Toast.makeText(ForgotPasswordActivity.this, e.toString(), Toast.LENGTH_LONG).show();
                                }
查看更多
登录 后发表回答