我试图做一个工具类,但是没有用,如果有人知道我错过了什么,请“告诉我”。
Obs:restOfUrl和siteUrl是该站点的路径。
//我正在调用util类:
NetworkManager networkManager = new NetworkManager(getApplicationContext());
networkManager.requestStringVolley("/restOfTheUrl");
// util类:
public class NetworkManager {
String prefUrl = "siteUrl";
RequestQueue mRequestQueue;
Context contextNet;
private static final String TAG = NetworkManager.class.getSimpleName();
public NetworkManager(Context context){
contextNet = context;
}
public void requestStringVolley(String posUrl){
mRequestQueue = Volley.newRequestQueue(contextNet);
String url = prefUrl + posUrl;
Response.Listener<String> listener = new Response.Listener<String>(){
@Override
public void onResponse(String response) {
sucessResponse(response);
}
};
Response.ErrorListener listenerError = new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error) {
errorResponse(error);
}
};
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, listener, listenerError);
mRequestQueue.add(stringRequest);
}
public void sucessResponse(String response){
Log.v(TAG, "Sucess");
}
public void errorResponse(VolleyError errorResponse){
Log.v(TAG, "Error");
}
}
// - 错误:
04-13 19:18:45.096 23880-23880/com.dev.kamui.patrulhacomunitaria E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.dev.kamui.patrulhacomunitaria, PID: 23880
java.lang.NullPointerException
at com.android.volley.Request.<init>(Request.java:136)
at com.android.volley.toolbox.StringRequest.<init>(StringRequest.java:43)
at com.dev.kamui.patrulhacomunitaria.NetworkManager$override.requestStringVolley(NetworkManager.java:41)
at com.dev.kamui.patrulhacomunitaria.NetworkManager$override.access$dispatch(NetworkManager.java)
at com.dev.kamui.patrulhacomunitaria.NetworkManager.requestStringVolley(NetworkManager.java:0)
at com.dev.kamui.patrulhacomunitaria.Pagina_Principal$1.onClick(Pagina_Principal.java:50)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
使用它:
sucessResponse(response.toString());
errorResponse(error);
public void sucessResponse(String response){
Log.v(TAG, response);
}
public void errorResponse(VolleyError errorResponse){
Log.v(TAG, "Error: " + errorResponse.getMessage());
}
我不知道为什么我必须这样做才能工作...但是知道它有效并且这是我需要的