我是android的新手,我正在进行登录和密码活动,如果你忘记了密码,那么在输入正确的电子邮件后,服务器会将密码发送到textview,应该设置为settext.i制作了php文件但是我无法在android中进行编码..我希望数据通过凌空。 这是我的php文件:
<?php
include ('config.php');
$sql = "SELECT * FROM UserInfo WHERE email='".$email."'";
$r = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($r)){
array_push($result,array(
'password'=>$row['password'],
));
}
echo json_encode(array('result'=>$result));
mysqli_close($con);
?>
这是我的ForgotPassword活动:
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);
}
}
}
这是我的xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.zeba.broccoli.Login">
<!-- Login progress -->
<LinearLayout
android:gravity="center_horizontal"
android:orientation="vertical"
android:id="@+id/login_form"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:src="@drawable/logobr"
android:layout_width="wrap_content"
android:layout_height="72dp"
android:layout_marginTop="40dp"
android:layout_gravity="center_horizontal" />
<LinearLayout
android:id="@+id/email_login_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="vertical">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="@+id/feml"
android:layout_width="match_parent"
android:hint="email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:singleLine="true"
android:background="@drawable/rounded_edittext"
android:layout_height="40dp"
android:paddingLeft="5dp"
android:layout_marginBottom="20dip"/>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<Button
android:id="@+id/sbtn"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Submit"
android:background="@drawable/mybutton"
android:textColor="#fff"
android:textStyle="bold"
android:elevation="0dp" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:weightSum="1" >
<TextView
android:id="@+id/fpss"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:gravity="center"
android:layout_marginTop="16dp"
android:text="Your Password is:" />
<TextView
android:id="@+id/pas"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:gravity="center"
android:layout_marginTop="16dp"
/>
我没有在logcat中收到任何错误,但是当我在设备上运行它时显示
jsonException....Index 0 out of 0 range[0...0]
答案 0 :(得分:1)
您的PHP
代码正在以JSON
格式发送密码,首先解码代码中的JSON
并获取密码:
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
// UPDATED HERE
String password = jsonObject.getString("password");
femail.setText(password, TextView.BufferType.EDITABLE);
}
catch (JSONException e) {
Toast.makeText(getContext(), e.toString(), Toast.LENGTH_LONG).show();
}
},
PHP
<?php
include ('config.php');
// updated here, value is mapped
$email = $_POST['email'];
$sql = "SELECT * FROM UserInfo WHERE email='".$email."'";
$r = mysqli_query($con,$sql);
//checking if email is valid or not
if(mysqli_fetch_array($r) == NULL){
echo "Invalid email";
}
else{
$result = array();
while($row = mysqli_fetch_array($r)){
array_push($result,array(
'password'=>$row['password'],
));
}
echo json_encode(array('result'=>$result));
}
mysqli_close($con);
?>
答案 1 :(得分:0)
使用JsonObjectRequest而不是StringRequest
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, json, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObjectresponse) {
// you code here
}
}
, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
// add your header here
return headers;
}
};