我花了数小时来调试这个问题,我觉得我真的很接近要解决这个问题,但是我无法弄清这个错误。我知道我的URL是正确的,并且我的PHP代码有效。我通过将URL放入运行该应用程序的手机的浏览器中对此进行了验证,并在结果页面的主HTML主体中将其返回给我正确的JSON对象。另外,如果我将PHP代码设置为有错误(我未定义var),则该代码将显示为登录响应,并且截击不会出错。
因此,我知道一个事实,PHP代码返回了一个有效的响应,并且我正在与服务器建立有效的连接,因此不存在该错误。只要PHP有效,则在收到来自PHP的响应之前会出现Volley错误。排球一定有问题,但我不知道这是什么。
其他可能有用的细节: -无论出于何种原因,它似乎都两次调用了getParams()。
下面是整个字符串请求,错误和App Singleton的代码。
private void loginUser( final String email, final String password) {
// Tag used to cancel the request
String cancel_req_tag = "login";
progressDialog.setMessage("Logging you in...");
showDialog();
StringRequest strReq = new StringRequest(Request.Method.POST, URL_FOR_LOGIN, new Response.Listener<String>()
{
@Override
public void onResponse(String response) {
Log.d(TAG, "Login Response: " + response);
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
String user = jObj.getJSONObject("user").getString("name");
// Launch User activity
Intent intent = new Intent(
LoginActivity.this,
UserActivity.class);
intent.putExtra("username", user);
startActivity(intent);
finish();
} else {
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Volley Error in login Activity: " + error.getMessage());
for(int i = 0; i<error.getStackTrace().length; i++)
{
Log.e(TAG, "Class name for Stack Trace element " + i +": " + error.getStackTrace()[i].getClassName());
Log.e(TAG, "File name for Stack Trace element " + i +": " + error.getStackTrace()[i].getFileName());
Log.e(TAG, "Line Number for Stack Trace element " + i +": " + error.getStackTrace()[i].getLineNumber());
Log.e(TAG, "Method name for Stack Trace element " + i +": " + error.getStackTrace()[i].getMethodName());
Log.e(TAG, "Result from isNativeMethod " + i +": " + error.getStackTrace()[i].isNativeMethod());
}
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
})
{
@Override
protected Map<String, String> getParams() {
// Posting params to login url
Map<String, String> params = new HashMap<String, String>();
params.put("email", email);
params.put("password", password);
return params;
}
};
// Adding request to request queue
AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(strReq,cancel_req_tag);
}
这是我收到的错误消息: E / LoginActivity:登录活动中出现Volley错误:null
Class name for Stack Trace element 0: com.android.volley.toolbox.BasicNetwork
File name for Stack Trace element 0: BasicNetwork.java
Line Number for Stack Trace element 0: 173
Method name for Stack Trace element 0: performRequest
Result from isNativeMethod 0: false
Class name for Stack Trace element 1: com.android.volley.NetworkDispatcher
File name for Stack Trace element 1: NetworkDispatcher.java
Line Number for Stack Trace element 1: 131
Method name for Stack Trace element 1: processRequest
Result from isNativeMethod 1: false
Class name for Stack Trace element 2: com.android.volley.NetworkDispatcher
File name for Stack Trace element 2: NetworkDispatcher.java
Line Number for Stack Trace element 2: 111
Method name for Stack Trace element 2: processRequest
Result from isNativeMethod 2: false
Class name for Stack Trace element 3: com.android.volley.NetworkDispatcher
File name for Stack Trace element 3: NetworkDispatcher.java
Line Number for Stack Trace element 3: 90
Method name for Stack Trace element 3: run
Result from isNativeMethod 3: false
public class AppSingleton {
private static AppSingleton mAppSingletonInstance;
private RequestQueue mRequestQueue;
private static Context mContext;
private AppSingleton(Context context) {
mContext = context;
mRequestQueue = getRequestQueue();
}
public static synchronized AppSingleton getInstance(Context context) {
if (mAppSingletonInstance == null) {
mAppSingletonInstance = new AppSingleton(context);
}
return mAppSingletonInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mContext.getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req,String tag) {
req.setTag(tag);
getRequestQueue().add(req);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycallstracker.mycallstracker">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".RegisterActivity"/>
<activity android:name=".UserActivity" />
</application>