我是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中收到错误我只是在移动设备中获取此错误 jsonException。索引0的0范围[0 ... 0]
这是我的json回复:
{
"password": ""
}
答案 0 :(得分:1)
我得到了答案...我的PHP代码很好,我在上面发布的问题是json对象和json数组,因为@Pier Giorgio Misley告诉我。所以这是我的更新活动:
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();
}