REST API在Postman中给出了正确的响应,但给出了Volley ServerError

时间:2019-03-24 07:33:48

标签: android rest android-volley server-error

我正在将图像数组发送到REST API,该API在数字化后返回表单字段。我的应用程序具有最低API级别21。我正在使用Android Volley向REST API发送数据或从REST API接收数据。

该代码可完美地用于单个图像,但是当发送多个图像时,该代码将失败。这里有一些答案建议清除队列中的缓存,但是它已经存在于代码中,并不能解决问题。

此外,后端代码也没有问题,因为它在Postman中工作正常,但响应在Android中失败。现在,我正在棉花糖上尝试。它很少起作用。该API似乎不是问题。


JSON请求:

{

    "imageFormat": "jpeg",

    "formType": "dummyForm",

    "images": 
    [
          "//base64 of image1", 
          "//base64 of image2", //as many as required       
    ]
}

JSON响应:

{
   "dummyLines": // Response from all images' form fields is in this array
    [
        {
            "confidence": "high",
            "key": "FIRST NAME :",
            "value": "ABC"
        },
        ...
        // The above set is repeated for all form fields
    ],
    "dummyStatus": "Succeeded",
    "message": "none"
}

编辑:

这些是处理请求和响应的类:

DummyRequest

public class DummyRequest
{
    String imageFormat;
    String formType;
    String images[];
}

DummyImageResponse

public class DummyImageResponse implements Serializable
{
    ArrayList<FieldsJson> dummyLines = new ArrayList<>();
    String dummyStatus;
}

FieldsJson

public class FieldsJson implements Serializable
{
    String key;
    String value;
    String confidence;
}

编辑

此代码将Bitmap图像编码为Base64

for(int i=0; i<cameraImageAdapter.getCount(); i++)
{
    Bitmap bitmap = BitmapFactory.decodeFile(capturedImagePathList.get(i));
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);

    byte[] imageByteArray = byteArrayOutputStream.toByteArray();
    cameraEncodedImages[i] = Base64.encodeToString(imageByteArray, Base64.DEFAULT);
}

编辑

我在onErrorResponse方法中包含了一些其他代码,并且获得了以下堆栈跟踪。 这是调用API的代码:

ResponseHandler responseHandler = new ResponseHandler(
                API_URL,
                DummyImageResponse.class,
                request,
                new HashMap<String, String>(),
                new Response.Listener<DummyImageResponse>() {
                    @Override
                    public void onResponse(DummyImageResponse response) {
                        Log.e("Success response", response.toString());

                        // Call new activity to display the result
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("Error response", error.toString());
                        NetworkResponse errorResponse = error.networkResponse;
                        if(error instanceof ServerError && errorResponse != null)
                        {
                            try {
                                Log.e("In error response", "Response not null");
                                String response = new String(errorResponse.data,
                                        HttpHeaderParser.parseCharset(errorResponse.headers, "UTF-8"));
                                Log.e("Parsed response", response);
                            }
                            catch (UnsupportedEncodingException e)
                            {
                                Log.e("In error response", e.getMessage());
                            }
                        }
                        else
                            Log.e("In error response", "Response is null");

                        // Call new activity to display error message
                    }
                }
        );

        RequestQueue queue = Volley.newRequestQueue(context);
        responseHandler.setRetryPolicy(new DefaultRetryPolicy(
                50000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        queue.getCache().clear();       // Clear the queue
        queue.add(responseHandler);     // Request is sent to the REST API


预期输出必须包含表单中的所有字段。但是,当涉及多个图像时,JSON响应很可能由于某种原因而损坏,并且由于某些ServerError库解析JSON而可能会生成glassfish,我读到了。我不确定。

可能是因为Android和Postman处理JSON数据略有不同。


这是我得到的确切错误消息:

E/Volley: [18942] BasicNetwork.performRequest: Unexpected response code 500 for **URL goes here**
E/Error response: com.android.volley.ServerError
E/In error response: Response not null
E/Parsed response: <!doctype html><html lang="en"><head><title>HTTP Status 500 – Internal Server Error</title><style type="text/css">h1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} h2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} h3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} body {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} b {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} p {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;} a {color:black;} a.name {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 500 – Internal Server Error</h1><hr class="line" /><p><b>Type</b> Exception Report</p><p><b>Message</b> java.lang.StringIndexOutOfBoundsException: String index out of range: -2</p><p><b>Description</b> The server encountered an unexpected condition that prevented it from fulfilling the request.</p><p><b>Exception</b></p><pre>javax.servlet.ServletException: java.lang.StringIndexOutOfBoundsException: String index out of range: -2
        org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:408)
        org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
        org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:365)
        org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:318)
        org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:205)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    </pre><p><b>Root Cause</b></p><pre>java.lang.StringIndexOutOfBoundsException: String index out of range: -2
        java.lang.String.substring(String.java:1967)
        com.example.ICRAPI.GetGender.main(GetGender.java:21)
        com.example.ICRAPI.dummyForm.main(dummyForm.java:178)
        sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        java.lang.reflect.Method.invoke(Method.java:498)
        org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.lambda$static$0(ResourceMethodInvocationHandlerFactory.java:52)
        org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:124)
        org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:167)
        org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$TypeOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:219)
        org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:79)
        org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:469)
        org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:391)
        org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:80)
        org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:253)
        org.glassfish.jersey.internal.Errors$1.call(Errors.java:248)
        org.glassfish.jersey.internal.Errors$1.call(Errors.java:244)
        org.glassfish.jersey.internal.Errors.process(Errors.java:292)
        org.glassfish.jersey.internal.Errors.process(Errors.java:274)
        org.glassfish.jersey.internal.Errors.process(Errors.java:244)
        org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:265)
        org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:232)
        org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:679)
        org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:392)
        org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:346)
        org.glassfish.jersey.servlet.ServletConta

0 个答案:

没有答案