带有 Volley 的 HTTP Post 获取 NullPointerException:尝试从字段 'byte[] c.a.b.l.b' 读取
HTTP Post with Volley gets NullPointerException: Attempt to read from field 'byte[] c.a.b.l.b'
我使用 volley 向网络服务发送 POST 请求。它工作得很好,但有时,我不知道为什么,我得到这个错误,我不知道原因以及如何控制它。我启用了 ACRA,这是关于我得到的错误的主要信息:
....
02-25 05:35:52.952 D\/InputMethodManager( 9979): prepareNavigationBarInfo() DecorView@72d70fd[ExportarDatos]
02-25 05:35:52.952 D\/InputMethodManager( 9979): getNavigationBarColor() -855310
02-25 05:35:52.968 E\/ViewRootImpl( 9979): sendUserActionEvent() mView returned.
02-25 05:35:53.194 D\/VOLLEY BODY SEND( 9979): {\"data\":{\"type\":.... rest of my json object}}
02-25 05:35:53.197 I\/System.out( 9979): (HTTPLog)-Static: isSBSettingEnabled false
02-25 05:35:53.197 I\/System.out( 9979): (HTTPLog)-Static: isSBSettingEnabled false
02-25 05:35:56.043 I\/System.out( 9979): (HTTPLog)-Static: isSBSettingEnabled false
02-25 05:35:56.043 I\/System.out( 9979): (HTTPLog)-Static: isSBSettingEnabled false
02-25 05:36:01.196 E\/VOLLEY ( 9979): c.a.b.t
02-25 05:36:01.209 D\/AndroidRuntime( 9979): Shutting down VM
--------- beginning of crash
02-25 05:36:01.211 E\/AndroidRuntime( 9979): FATAL EXCEPTION: main
02-25 05:36:01.211 E\/AndroidRuntime( 9979): Process: com.mypack.myapp, PID: 9979
02-25 05:36:01.211 E\/AndroidRuntime( 9979): **java.lang.NullPointerException: Attempt to read from field 'byte[] c.a.b.l.b' on a null object reference**
02-25 05:36:01.211 E\/AndroidRuntime( 9979): \tat com.mypack.myapp.ExportarDatos$c.a(ExportarDatos.java:2)
02-25 05:36:01.211 E\/AndroidRuntime( 9979): \tat c.a.b.o.a(Request.java:7)
02-25 05:36:01.211 E\/AndroidRuntime( 9979): \tat c.a.b.g$b.run(ExecutorDelivery.java:6)
02-25 05:36:01.211 E\/AndroidRuntime( 9979): \tat android.os.Handler.handleCallback(Handler.java:883)
02-25 05:36:01.211 E\/AndroidRuntime( 9979): \tat android.os.Handler.dispatchMessage(Handler.java:100)
02-25 05:36:01.211 E\/AndroidRuntime( 9979): \tat android.os.Looper.loop(Looper.java:237)
02-25 05:36:01.211 E\/AndroidRuntime( 9979): \tat android.app.ActivityThread.main(ActivityThread.java:8107)
02-25 05:36:01.211 E\/AndroidRuntime( 9979): \tat java.lang.reflect.Method.invoke(Native Method)
02-25 05:36:01.211 E\/AndroidRuntime( 9979): \tat com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
02-25 05:36:01.211 E\/AndroidRuntime( 9979): \tat com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
02-25 05:36:01.213 E\/ACRA ( 9979): ACRA caught a NullPointerException for com.mypack.myapp
02-25 05:36:01.213 E\/ACRA ( 9979): java.lang.NullPointerException: Attempt to read from field 'byte[] c.a.b.l.b' on a null object reference
02-25 05:36:01.213 E\/ACRA ( 9979): \tat com.mypack.myapp.ExportarDatos$c.a(ExportarDatos.java:2)
02-25 05:36:01.213 E\/ACRA ( 9979): \tat c.a.b.o.a(Request.java:7)
02-25 05:36:01.213 E\/ACRA ( 9979): \tat c.a.b.g$b.run(ExecutorDelivery.java:6)
02-25 05:36:01.213 E\/ACRA ( 9979): \tat android.os.Handler.handleCallback(Handler.java:883)
02-25 05:36:01.213 E\/ACRA ( 9979): \tat android.os.Handler.dispatchMessage(Handler.java:100)
02-25 05:36:01.213 E\/ACRA ( 9979): \tat android.os.Looper.loop(Looper.java:237)
02-25 05:36:01.213 E\/ACRA ( 9979): \tat android.app.ActivityThread.main(ActivityThread.java:8107)
02-25 05:36:01.213 E\/ACRA ( 9979): \tat java.lang.reflect.Method.invoke(Native Method)
02-25 05:36:01.213 E\/ACRA ( 9979): \tat com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
02-25 05:36:01.213 E\/ACRA ( 9979): \tat com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)",
...
这是向网络服务发送 POST/PATCH 请求的函数。正如我所说,它大部分时间都运行良好,但有时我会出错,我不知道什么时候也不知道为什么:
private void SendRequest(String url, JSONObject jsonBody, boolean lPost) {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(this);
}
final String mRequestBody = jsonBody.toString();
Log.d("VOLLEY BODY SEND", mRequestBody);
int nMetodo = Request.Method.POST;
if (!lPost) nMetodo = Request.Method.PATCH;
StringRequest stringRequest = new StringRequest(nMetodo, urlbase + url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("VOLLEY", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
error.networkResponse.data.toString();
try {
String body = new String(error.networkResponse.data, "UTF-8");
Log.d("VOLLEY_ERROR", body);
txtError.setText(body);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Accept", "application/vnd.api+json");
params.put("Content-Type", "application/vnd.api+json");
params.put("api-key", cparam);
return params;
}
@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);
// can get more details such as response.headers
Log.d("VOLLEY", responseString);
String json = "";
try {
json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
jsonObjectRcv = new JSONObject(json);
// Process received object
handler.postDelayed(new Runnable() {
@Override
public void run() {
ProcessResponse(jsonObjectRcv);
}
}, 50);
} catch (Exception e) {
Log.d("VOLLEY", e.getMessage());
}
}
return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
}
};
// Add to queue
mRequestQueue.add(stringRequest);
}
urlbase
是网络服务的主要 url。 url 参数是 POST/PATCH 的端点+资源。 jsonBody 是一个 JSON 对象,其中包含我想要 POST 或 PATH 的信息,它是正确的。我敢肯定,因为它包含在 ACRA 的行中:
02-25 05:35:53.194 D\/VOLLEY BODY SEND( 9979): {\"data\":{\"type\":.... rest of my json object}}
我想我已经添加了关于我的问题的所有信息以获得帮助,但请问我是否应该添加更多信息,我会更新我的问题。我不知道是什么抛出 Nullpointer 异常。
我想捕获错误并让用户重试。现在,启动 ACRA 错误,应用程序停止运行。
经过大量测试、失败和研究,问题是 e.printStackTrace();
。我已经删除了这一行并用 Log.e 更改了它,问题就解决了。
我使用 volley 向网络服务发送 POST 请求。它工作得很好,但有时,我不知道为什么,我得到这个错误,我不知道原因以及如何控制它。我启用了 ACRA,这是关于我得到的错误的主要信息:
....
02-25 05:35:52.952 D\/InputMethodManager( 9979): prepareNavigationBarInfo() DecorView@72d70fd[ExportarDatos]
02-25 05:35:52.952 D\/InputMethodManager( 9979): getNavigationBarColor() -855310
02-25 05:35:52.968 E\/ViewRootImpl( 9979): sendUserActionEvent() mView returned.
02-25 05:35:53.194 D\/VOLLEY BODY SEND( 9979): {\"data\":{\"type\":.... rest of my json object}}
02-25 05:35:53.197 I\/System.out( 9979): (HTTPLog)-Static: isSBSettingEnabled false
02-25 05:35:53.197 I\/System.out( 9979): (HTTPLog)-Static: isSBSettingEnabled false
02-25 05:35:56.043 I\/System.out( 9979): (HTTPLog)-Static: isSBSettingEnabled false
02-25 05:35:56.043 I\/System.out( 9979): (HTTPLog)-Static: isSBSettingEnabled false
02-25 05:36:01.196 E\/VOLLEY ( 9979): c.a.b.t
02-25 05:36:01.209 D\/AndroidRuntime( 9979): Shutting down VM
--------- beginning of crash
02-25 05:36:01.211 E\/AndroidRuntime( 9979): FATAL EXCEPTION: main
02-25 05:36:01.211 E\/AndroidRuntime( 9979): Process: com.mypack.myapp, PID: 9979
02-25 05:36:01.211 E\/AndroidRuntime( 9979): **java.lang.NullPointerException: Attempt to read from field 'byte[] c.a.b.l.b' on a null object reference**
02-25 05:36:01.211 E\/AndroidRuntime( 9979): \tat com.mypack.myapp.ExportarDatos$c.a(ExportarDatos.java:2)
02-25 05:36:01.211 E\/AndroidRuntime( 9979): \tat c.a.b.o.a(Request.java:7)
02-25 05:36:01.211 E\/AndroidRuntime( 9979): \tat c.a.b.g$b.run(ExecutorDelivery.java:6)
02-25 05:36:01.211 E\/AndroidRuntime( 9979): \tat android.os.Handler.handleCallback(Handler.java:883)
02-25 05:36:01.211 E\/AndroidRuntime( 9979): \tat android.os.Handler.dispatchMessage(Handler.java:100)
02-25 05:36:01.211 E\/AndroidRuntime( 9979): \tat android.os.Looper.loop(Looper.java:237)
02-25 05:36:01.211 E\/AndroidRuntime( 9979): \tat android.app.ActivityThread.main(ActivityThread.java:8107)
02-25 05:36:01.211 E\/AndroidRuntime( 9979): \tat java.lang.reflect.Method.invoke(Native Method)
02-25 05:36:01.211 E\/AndroidRuntime( 9979): \tat com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
02-25 05:36:01.211 E\/AndroidRuntime( 9979): \tat com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)
02-25 05:36:01.213 E\/ACRA ( 9979): ACRA caught a NullPointerException for com.mypack.myapp
02-25 05:36:01.213 E\/ACRA ( 9979): java.lang.NullPointerException: Attempt to read from field 'byte[] c.a.b.l.b' on a null object reference
02-25 05:36:01.213 E\/ACRA ( 9979): \tat com.mypack.myapp.ExportarDatos$c.a(ExportarDatos.java:2)
02-25 05:36:01.213 E\/ACRA ( 9979): \tat c.a.b.o.a(Request.java:7)
02-25 05:36:01.213 E\/ACRA ( 9979): \tat c.a.b.g$b.run(ExecutorDelivery.java:6)
02-25 05:36:01.213 E\/ACRA ( 9979): \tat android.os.Handler.handleCallback(Handler.java:883)
02-25 05:36:01.213 E\/ACRA ( 9979): \tat android.os.Handler.dispatchMessage(Handler.java:100)
02-25 05:36:01.213 E\/ACRA ( 9979): \tat android.os.Looper.loop(Looper.java:237)
02-25 05:36:01.213 E\/ACRA ( 9979): \tat android.app.ActivityThread.main(ActivityThread.java:8107)
02-25 05:36:01.213 E\/ACRA ( 9979): \tat java.lang.reflect.Method.invoke(Native Method)
02-25 05:36:01.213 E\/ACRA ( 9979): \tat com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
02-25 05:36:01.213 E\/ACRA ( 9979): \tat com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)",
...
这是向网络服务发送 POST/PATCH 请求的函数。正如我所说,它大部分时间都运行良好,但有时我会出错,我不知道什么时候也不知道为什么:
private void SendRequest(String url, JSONObject jsonBody, boolean lPost) {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(this);
}
final String mRequestBody = jsonBody.toString();
Log.d("VOLLEY BODY SEND", mRequestBody);
int nMetodo = Request.Method.POST;
if (!lPost) nMetodo = Request.Method.PATCH;
StringRequest stringRequest = new StringRequest(nMetodo, urlbase + url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("VOLLEY", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
error.networkResponse.data.toString();
try {
String body = new String(error.networkResponse.data, "UTF-8");
Log.d("VOLLEY_ERROR", body);
txtError.setText(body);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Accept", "application/vnd.api+json");
params.put("Content-Type", "application/vnd.api+json");
params.put("api-key", cparam);
return params;
}
@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);
// can get more details such as response.headers
Log.d("VOLLEY", responseString);
String json = "";
try {
json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
jsonObjectRcv = new JSONObject(json);
// Process received object
handler.postDelayed(new Runnable() {
@Override
public void run() {
ProcessResponse(jsonObjectRcv);
}
}, 50);
} catch (Exception e) {
Log.d("VOLLEY", e.getMessage());
}
}
return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
}
};
// Add to queue
mRequestQueue.add(stringRequest);
}
urlbase
是网络服务的主要 url。 url 参数是 POST/PATCH 的端点+资源。 jsonBody 是一个 JSON 对象,其中包含我想要 POST 或 PATH 的信息,它是正确的。我敢肯定,因为它包含在 ACRA 的行中:
02-25 05:35:53.194 D\/VOLLEY BODY SEND( 9979): {\"data\":{\"type\":.... rest of my json object}}
我想我已经添加了关于我的问题的所有信息以获得帮助,但请问我是否应该添加更多信息,我会更新我的问题。我不知道是什么抛出 Nullpointer 异常。
我想捕获错误并让用户重试。现在,启动 ACRA 错误,应用程序停止运行。
经过大量测试、失败和研究,问题是 e.printStackTrace();
。我已经删除了这一行并用 Log.e 更改了它,问题就解决了。