Android 字符串类型的json错误值无法转换为jsonobject
我正在使用一个PHPWeb服务来创建一个登录页面。但在JSON响应中,在main JSON()之外有一个字符串。我试图用这个作为引用来解析它,但是我得到了这个JSON错误。有人能帮我在截击请求中解析这个JSON吗Android 字符串类型的json错误值无法转换为jsonobject,android,json,android-volley,Android,Json,Android Volley,我正在使用一个PHPWeb服务来创建一个登录页面。但在JSON响应中,在main JSON()之外有一个字符串。我试图用这个作为引用来解析它,但是我得到了这个JSON错误。有人能帮我在截击请求中解析这个JSON吗 public类MainActivity扩展了AppCompatActivity{ 私有文本用户名、密码; 私人按钮登录; 私有静态最终字符串登录=”http://demo.example.net/login.php"; @凌驾 创建时受保护的void(Bundle savedInst
public类MainActivity扩展了AppCompatActivity{
私有文本用户名、密码;
私人按钮登录;
私有静态最终字符串登录=”http://demo.example.net/login.php";
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
用户名=(EditText)findViewById(R.id.ET1);
密码=(EditText)findViewById(R.id.ET2);
登录=(按钮)findviewbyd(R.id.btn);
login.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
字符串名称=username.getText().toString();
字符串pass=password.getText().toString();
如果(!name.isEmpty()&&!pass.isEmpty()){
尝试登录();
}
}
});
}
私有void attemptlogin(){
StringRequest StringRequest=新建StringRequest(Request.Method.POST,LOGIN,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
Toast.makeText(getApplicationContext(),response,Toast.LENGTH_LONG.show();
试一试{
JSONObject jObj=新的JSONObject(响应);
JSONObject phone=jObj.getJSONObject(“test123”);
字符串状态=phone.getString(“成功”);
//现在检查状态值
如果(状态等于(“0”)){
Toast.makeText(getApplicationContext(),“出现错误!请重试。”,Toast.LENGTH_LONG.show();
}else if(状态等于(“1”)){
Toast.makeText(getApplicationContext(),“Success”,Toast.LENGTH_LONG.show();
//startActivity(新意图(getApplicationContext(),SecondActivity.class));
//完成();
}否则{
String errorMsg=jObj.getString(“error_msg”);
Toast.makeText(getApplicationContext(),errorMsg,Toast.LENGTH_LONG.show();
}
}捕获(JSONException e){
//JSON错误
e、 printStackTrace();
Toast.makeText(getApplicationContext(),“Json错误:+e.getMessage(),Toast.LENGTH_LONG).show();
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Toast.makeText(getApplicationContext(),“VolleyError”+error.toString(),Toast.LENGTH_LONG).show();
}
}) {
@凌驾
受保护的映射getParams(){
Map params=新的HashMap();
put(“username”,username.getText().toString());
put(“password”,password.getText().toString());
返回参数;
}
};
RequestQueue RequestQueue=Volley.newRequestQueue(getApplicationContext());
添加(stringRequest);
}
}
这是因为您试图将整个字符串转换为Json对象,但无法完成
在你的回答中试试这个
int index=response.indexOf("{");
String jsonString= st.substring(index);
JSONObject jObj = new JSONObject(jsonString);
String status = jObj.getString("success");
字符串只有在以“{”开头时才能转换为JsonObject。感谢您的回复和解释。在
String jsonString=st.substring(index);
?@SomnathPal这里的“st”是什么?