【问题标题】:Not getting value from server into my Textview using android没有使用 android 从服务器获取价值到我的 Textview
【发布时间】:2017-05-18 18:04:00
【问题描述】:

我是 android 新手,如果我选择忘记密码链接,它应该转到下一个活动,如果我写正确的电子邮件,那么它应该 setText 来自服务器的正确密码。但我没有从服务器获得价值。 这是我的 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 中没有收到错误,我只是在移动设备中收到此错误 json异常。索引 0 超出 0 范围[0...0]

这是我的 json 响应:

{
    "password": ""
}

【问题讨论】:

  • 通过调试,第一个对象后第一个数组为空还是被填满?
  • android新手..不知道如何调试
  • .. 好的,所以你最好回到教程阶段.. 调试是学习的第一个任务。在谷歌上搜索“如何在 android studio 上调试”。无论如何,在程序的顶部中间有一个名为“调试”的按钮,通过单击左侧的一行,您将添加一个断点等等。但真的先看一些教程
  • 我这样说只是因为如果您不知道如何调试,我们很难为您提供帮助,因为您不会知道程序的状态
  • {"result":[]} 我从调试中得到它

标签: php android mysql android-volley


【解决方案1】:

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

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

【讨论】:

  • 是的,这是解析 json 的正确方法。主要问题在于解析 json .. 解析依赖于 json 响应的结构。
  • 你知道如何将警报对话框的值发送到数据库...你试过这种类型的东西吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-07
  • 1970-01-01
  • 1970-01-01
  • 2013-11-17
  • 2021-12-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多