android - 如何解决使用 volley 解析登录 json 的问题?
问题描述
public void userLogin() {
try {
String URL = "http://influence360.in/orezmiapp/index.php/mobile/login";
System.out.println("Arun URL :- " + URL);
final JSONObject jsonBody = new JSONObject();
jsonBody.put("type", "2");
jsonBody.put("email", email);
jsonBody.put("password", password);
final String mRequestBody = jsonBody.toString();
Log.e("JSON String is :- ", " " + mRequestBody);
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("LOG_VOLLEY", response);
try {
Toast.makeText(activity, "loginactivity entered", Toast.LENGTH_LONG).show();
JSONObject jsonObject = new JSONObject(response);
int status = jsonObject.getInt("status");
Toast.makeText(activity, status, Toast.LENGTH_LONG).show();
if (status == 1000) {
JSONObject data = jsonBody.getJSONObject("data");
//sessionManager.setUserid(data.getString("Cust_id"));
// sessionManager.setBirthday(data.getString("Dob"));
sessionManager.setUserfirstname(data.getString("Cust_Name"));
startActivity(new Intent(LoginScreen.this, Profile.class));
finish();
} else {
Toast.makeText(activity, "Hello", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(activity, e.toString(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}, new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
Log.e("LOG_VOLLEY", error.toString());
}
})
{
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
return headers;
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
return null;
}
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String responseString = "";
if (response != null) {
responseString = String.valueOf(response.statusCode);
Log.e("Response String : -", " " + responseString);
}
return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
}
};
RequestQueue requestQueue = Volley.newRequestQueue(activity);
requestQueue.add(stringRequest);
stringRequest.setRetryPolicy(new
DefaultRetryPolicy(60000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
}
我得到类型 200 org.json.Integer 的错误值不能转换为 JSONObject。请帮我解决这个问题。我没有得到任何解决方案。提前谢谢你。请帮助我。如果有人提供解决方案,我将非常感激。提前谢谢你。如果有人有解决方案,请给我。
解决方案
推荐阅读
- python - 配置文本文件中没有值的“DEFAULT”部分
- python - 我可以从不同的 Python 类型存根文件中重新导出类型吗?
- reactjs - 在 Reactjs 中声明动态 const 变量类型
- r - 交互模式下的自引用 Rmd 文件
- azureservicebus - 有谁知道带有 TLS 1.2 的服务总线 1.1 将如何在负载平衡环境中工作?
- json - 斯卡拉。如何使用 json spray 解组选项值?
- python - Python 子进程从 houdini 运行 miniconda3/etc/profile.d/conda.sh
- apache-kafka - 如何对齐(延迟处理)来自两个主题的 Kafka 事件(通过内部属性)?或者如何顺序处理来自两个主题的事件?
- c# - 在 C# 命名空间中查找方法定义
- class - Dart 返回多个坐标