[英]How to fix org.json.JSONException: on Volley?
I am developing Login on android via connection with php (web). 我正在通过与php(web)的连接在Android上开发Login。
Code 码
package com.example.salmakhalil.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class LoginActivity extends AppCompatActivity {
private EditText email,password;
private Button btn_login;
private TextView link_regist;
private ProgressBar loading;
private static String URL_LOGIN="http://192.168.8.101/android_register_login/login.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
loading=findViewById(R.id.loading);
email=findViewById(R.id.email);
password=findViewById(R.id.password);
btn_login=findViewById(R.id.btn_login);
link_regist=findViewById(R.id.link_regist);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String mEmail=email.getText().toString().trim();
String mPass=password.getText().toString().trim();
if (!mEmail.isEmpty() || !mPass.isEmpty()){
Login(mEmail,mPass);
}
else {
email.setError("Please insert email");
password.setError("Please insert Password");
}
}
});
link_regist.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(LoginActivity.this, MainActivity.class));
}
});
}
private void Login(String email, String password){
loading.setVisibility(View.VISIBLE);
btn_login.setVisibility(View.GONE);
//I think here facing a problem //我认为这里面临一个问题
StringRequest stringRequest=new StringRequest(Request.Method.POST, URL_LOGIN,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObject=new JSONObject(response);
String success = jsonObject.getString("success");
JSONArray jsonArray=jsonObject.getJSONArray("login");
if (success.equals("1")) {
for (int i=0; i<jsonArray.length();i++){
JSONObject object = jsonArray.getJSONObject(i);
String name = object.getString("name").trim();
String email=object.getString( "email").trim();
Toast.makeText(LoginActivity.this,
"Success Login. \nYour Name : "
+name+"\nYour Email : "
+email, Toast.LENGTH_SHORT)
.show();
loading.setVisibility(View.GONE);
}
}
} catch (JSONException e) {
e.printStackTrace();
loading.setVisibility(View.GONE);
btn_login.setVisibility(View.VISIBLE);
Toast.makeText(LoginActivity.this, "Error!!! " +e.toString(),
Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(LoginActivity.this, "Error!! " +error.toString(),
Toast.LENGTH_SHORT).show();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("email", email);
params.put("password", password);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
When I click on the login Button, its showing thee error 当我单击登录按钮时,它显示您错误
org.json.JSONException: No value for success.
org.json.JSONException:成功没有价值。
I'm all the day trying to fix it. 我整天都在尝试修复它。 I have read other posts on Stack, no one helped me.
我已经阅读了Stack上的其他文章,没有人帮助过我。 I hope you can help me.
我希望你能帮助我。 Thanks.
谢谢。
Apparently the jsonObject
your receive in onResponse()
does not have a value for entry "success" 显然,
jsonObject
您收到onResponse()
不具有条目“成功”的值
You should use the method jsonObject.optString("success");
您应该使用
jsonObject.optString("success");
方法jsonObject.optString("success");
instead of jsonObject.getString("success");
而不是
jsonObject.getString("success");
optString("success")
returns the empty string ("") if the key "success" doesn't exist. 如果键“ success”不存在,则
optString("success")
返回空字符串(“”)。 getString("success")
on the other hand throws a JSONException. 另一方面,
getString("success")
抛出JSONException。
Can you share the response that is supposed to get. 您能否分享应该得到的答复。 I think the response doesn't contain any field name "success".
我认为响应中不包含任何字段名称“ success”。 Query on postman to get response otherwise talk to your back end developer.
查询邮递员以获取响应,否则请与您的后端开发人员联系。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.