![](/img/trans.png)
[英]why volley library not working on android 9(API 28) where as its working fine on android 8(API 27)?
[英]Working with Android Volley Library and API
我是Android Volley Library的新手。 我正在向API發送用戶名和密碼,API返回用戶詳細信息。 所以這是我在android中的LoginUser方法。
public void LoginUser(){
// Tag used to cancel the request
String tag_json_obj = "json_obj_req";
String url = "http://example.com/myfile/mobile_api/";
//final ProgressDialog pDialog = ProgressDialog.show(getParent(), "Please wait", "Login user");
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
if(response.getString(KEY_SUCCESS)!=null){
String res = response.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
JSONObject json_user = response.getJSONObject("user");
Toast.makeText(getApplicationContext(), "User Logged in.. " + json_user.getString("name"), Toast.LENGTH_LONG).show();
}
}
else if(response.getString(KEY_ERROR)!=null){
String res = response.getString(KEY_ERROR);
if(Integer.parseInt(res) == 1){
Toast.makeText(getApplicationContext(), response.getString("error_msg"), Toast.LENGTH_LONG).show();
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
//pDialog.hide();
}
Log.d(TAG, response.toString());
//pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Log.d(TAG, "Error: " + error.getMessage());
//pDialog.hide();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("tag", login_tag);
params.put("email", Email);
params.put("password", Pass);
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
}
這是我的API index.php文件
if ($tag == 'login') {
$email = $_POST['email'];
$password = $_POST['password'];
$user = $db->getUserByEmailAndPassword($email, $password);
if ($user != false) {
$response["success"] = 1;
$response["uid"] = $user["id"];
$response["user"]["name"] = $user["name"];
$response["user"]["phone"] = $user["contact_no"];
$response["user"]["email"] = $user["email"];
$response["user"]["reg_date"] = $user["reg_date"];
echo json_encode($response);
} else {
$response["error"] = 1;
$response["error_msg"] = "Incorrect email or password!";
echo json_encode($response);
}
但我從Json得到以下錯誤
07-31 16:58:27.307:E / JSON解析器(7506):解析數據org.json.JSONException:值類型java.lang.String的值無法轉換為JSONObject
那么這是使用Volley連接移動API和Android App的正確方法嗎? 或有人可以解釋我的原因嗎?
好吧,我想我已經找到問題了。 我收到該錯誤消息是因為如果標簽不正確,它將返回字符串“ Access Denied”,但會顯示“ Access Denied”消息。 所以這不是一個json對象,它只是一個字符串。 要解決該錯誤,可以使用String Request獲取字符串值。
StringRequest strReq = new StringRequest(Method.POST, //here change the method to POST
Const.URL_STRING_REQ, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, response.toString());
msgResponse.setText(response.toString());
hideProgressDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hideProgressDialog();
}
}){
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("tag", login_tag);
params.put("email", Email);
params.put("password", Pass);
return params;
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.