使用Android无法从服务器获取价值到我的Textview中 [英] Not getting value from server into my Textview using android

查看:89
本文介绍了使用Android无法从服务器获取价值到我的Textview中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android的新手,如果我选择忘记密码链接,它将进入下一个活动;如果我写了正确的电子邮件,则应该setText来自server的正确密码.但是我没有从server获得价值. 这是我的ForgotPassword.java:

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

}

这是我的php代码:

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

?>

我在logcat中没有收到错误,我只是在移动设备中得到了 jsonException.索引0超出0范围[0 ... 0]

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

这是我的json响应:

here is my json response:

{
    "password": ""
}

推荐答案

我得到了答案...我在上面发布的php代码很好,问题在于JSON对象和json数组,如@Pier Giorgio Misley告诉我的那样.所以这是我的更新活动:

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

这篇关于使用Android无法从服务器获取价值到我的Textview中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆