如何阅读多部分响应,从resteasy发送到android,使用okHttp?

时间:2017-02-15 04:18:08

标签: android jax-rs multipartform-data resteasy okhttp

我想从JBoss服务器上传文件列表(在我的情况下是图像)到android。 我是通过以下编写的代码来完成的:

@import url('https://fonts.googleapis.com/css?family=Sansita');
.font {
  font-family: 'Sansita', sans-serif;
}
p {
  font-family: 'Sansita', sans-serif;
}

在Android方面,我想阅读回复(多部分形式的文件)。我正在使用okHttp进行连接。 在互联网上搜索了很多我尝试了下面的代码来阅读多部分响应,但它不起作用。似乎没有从流中读取任何内容。

@GET
@Path("/report/{filename}")
@Produces({MediaType.MULTIPART_FORM_DATA})
public MultipartFormDataOutput getReport(@PathParam("filename") String filename, @Context HttpServletRequest request) {
    try {
        String token = request.getHeader("Authorization");
        List<File> file = processImage.getImage(filename,token);
        MultipartFormDataOutput output = new MultipartFormDataOutput();
        for(File image: file){
        System.out.println("class of this" +image + "MMMM" +image.exists());
        output.addPart(image, new MediaType("image","jpg"));
        }
        return output;

    } .....
      .......
}

任何人都可以帮我解决这个问题。我被困在这里2天。我做错了什么。

3 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

  • 创建一个在后台运行的异步任务
  • 使用okhttp3.Request创建请求
  • 创建OkHttpClient以发送请求并将响应保存在okHttp3响应中
  • 然后从okhttp3.ResponseBody获取响应主体并使用InputStreamReader和BufferedReader读取文件。

以下内容应该有效:

private class FileTask extends AsyncTask<Void, Void, Void> {
   private File file = null;
    @Override
    protected Void doInBackground(Void... params) {

        okhttp3.Request request = new okhttp3.Request.Builder()
                .url("Your URL for getReport()")
                .get()
                .build();
        OkHttpClient okHttpClient = new OkHttpClient.Builder().
                        connectTimeout(80, TimeUnit.SECONDS)
                        .writeTimeout(80, TimeUnit.SECONDS)
                        .readTimeout(80, TimeUnit.SECONDS)
                        .build();


        try {
            Response response = okHttpClient.newCall(request).execute();
            okhttp3.ResponseBody in = response.body();
            InputStream is = in.byteStream();
            InputStreamReader inputStreamReader = new InputStreamReader(is);
            BufferedReader buffReader = new BufferedReader(inputStreamReader);

            file = new File(Environment.getExternalStorageDirectory() + File.separator + "filename.txt"); //Your output file
            OutputStream output = new FileOutputStream(file);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(output);


            String line=buffReader.readLine();
            while ((line=buffReader.readLine()) != null) {
                output.write(line.getBytes());
                output.write('\n');
            }
            buffReader.close();
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        //do post execution steps
        }
    }
}

答案 1 :(得分:0)

试试这个:

 //first you should convert image into bytearray


 VolleyMultipartRequest multipartRequest = new VolleyMultipartRequest(Request.Method.POST, url, new Response.Listener<NetworkResponse>() {
        @Override
        public void onResponse(NetworkResponse response) {

            System.out.println(" response"+response.data);


            String resultResponse = new String(response.data);
            System.out.println("-------------  response-----------------"+resultResponse);

            try {
                JSONObject jsonObject = new JSONObject(resultResponse);
                sStatus = jsonObject.getString("status");


                } else {

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            dialog.dismiss();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            dialog.dismiss();

            NetworkResponse networkResponse = error.networkResponse;
            String errorMessage = "Unknown error";
            if (networkResponse == null) {
                if (error.getClass().equals(TimeoutError.class)) {
                    errorMessage = "Request timeout";
                } else if (error.getClass().equals(NoConnectionError.class)) {
                    errorMessage = "Failed to connect server";
                }
            } else {
                String result = new String(networkResponse.data);
                try {
                    JSONObject response = new JSONObject(result);
                    String status = response.getString("status");
                    String message = response.getString("message");

                    Log.e("Error Status", status);
                    Log.e("Error Message", message);

                    if (networkResponse.statusCode == 404) {
                        errorMessage = "Resource not found";
                    } else if (networkResponse.statusCode == 401) {
                        errorMessage = message + " Please login again";
                    } else if (networkResponse.statusCode == 400) {
                        errorMessage = message + " Check your inputs";
                    } else if (networkResponse.statusCode == 500) {
                        errorMessage = message + " Something is getting wrong";
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            Log.i("Error", errorMessage);
            error.printStackTrace();
        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<>();
            params.put("id",ID);
            System.out.println("id---------------"+ID);
            return params;
        }

        @Override
        protected Map<String, DataPart> getByteData() {
            Map<String, DataPart> params = new HashMap<>();
            params.put("image",new DataPart("imnage.jpg", byteArray));

            return params;
        }
    }

    AppController.getInstance().addToRequestQueue(multipartRequest);

}

答案 2 :(得分:0)

您是否可以将HttpLoggingInterceptor与setLevel(HttpLoggingInterceptor.Level.BODY)一起使用?我使用您的代码成功解析了多部分响应。但是,一旦我将级别从HEADERS更改为BODY,该代码就不再有效,并且就好像它不是从流中读取任何内容一样。我已经报告了这个问题:https://github.com/square/okhttp/issues/3269

顺便说一句,感谢给我示例代码,允许我用OkHttp解析多部分正文。对于其他可能出现的人,我不得不将其添加到我的maven pom.xml中:

    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.5.6</version>
    </dependency>