【发布时间】:2016-11-06 19:15:40
【问题描述】:
public void searchLoginInfo(View view) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, showUrl,null,new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray users = response.getJSONArray("users");
for (int i = 0; i < users.length(); i++){
JSONObject user = users.getJSONObject(i);
String username = user.getString("username").toLowerCase();
String password = user.getString("password".toLowerCase());
String retypedpassword = user.getString("retypedpassword");
String email = user.getString("email");
if (username.equals(myLoginList.get(0)) && password.equals(myLoginList.get(1))) {
Toast.makeText(LoginActivity.this, "Login Succesfull", Toast.LENGTH_LONG).show();
Intent send = new Intent(LoginActivity.this, WelcomeActivity.class);
startActivity(send);
break;
}else{
Toast.makeText(LoginActivity.this, "Login Failed!", Toast.LENGTH_LONG).show();
Intent send = new Intent(LoginActivity.this, LoginActivity.class);
startActivity(send);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley Error", error.toString());
NetworkResponse networkResponse = error.networkResponse;
if (networkResponse != null) {
Log.e("Status code", String.valueOf(networkResponse.statusCode));
}
}
});
requestQueue.add(jsonObjectRequest);
}
我正在使用 JSON 对象请求从我创建的数据库中获取信息。我可以使用我的 android 应用程序将数据插入数据库,但现在我想检查例如用户是否创建了用户帐户。 url 和 php 文件都可以正常工作。我在 onErrorResponse 方法中使用了日志来查看实际错误是什么,我得到了这个:E/Volley Error: com.android.volley.ParseError: org.json.JSONException: Value Connection of type java.lang .String 无法转换为 JSONObject。我不知道这似乎是什么问题。如果我的 Web 服务返回 json 数据,我还使用 postamn 检查过。Printscreen with the json data 有谁能够帮我?提前致谢
【问题讨论】:
-
听起来 Volley 无法转换为 json 无论您传递什么字符串。我们真的需要查看传递给 Volley 的内容,以确定它有什么问题。
-
@saywhatnow 我该怎么做?
-
在每一步,
Log.debug数据,这样您就可以看到您实际使用的内容。如果您不明白为什么某事以特定方式表现,请从小块中查看它。 :)
标签: php android json jsonobject