gpt4 book ai didi

php - 没有使用android从服务器获取值(value)到我的Textview

转载 作者:行者123 更新时间:2023-11-28 23:21:21 25 4
gpt4 key购买 nike

我是 android 的新手,如果我在这里选择忘记密码链接,它应该转到下一个 Activity ,如果我写正确的电子邮件,那么它应该从服务器设置文本正确的密码。但我没有从服务器获得值(value)。这是我的 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": ""
}

最佳答案

我得到了答案...我的 php 代码很好,我在上面发布了问题是 json 对象和 json 数组,正如@Pier Giorgio Misley 告诉我的那样。所以这是我的更新 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();
}

关于php - 没有使用android从服务器获取值(value)到我的Textview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41463967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com