The application every time when it accesses the server attaches a Json file to the request that Json all the data

 JSONObject data = new JSONObject(); data.put("1", 1); data.put("2", 2); data.put("3", 3); 

But now you need to send 30 MB of data with this file ...

I do not understand how to do it right? I understand that I can’t put 30 MB of bytes in Json ...

How to send such a request?

  • And whence infa that it does not work in the standard way? Any JSON limitations? - Yuriy SPb
  • one
    @Yuriy SPb well, it seems to me that it’s not right to shove in json 30 mb ... I found MultipartEntity here .... But I understand it seems like it is deprecated ... What say? - Aleksey Timoshchenko
  • one
    Well, this is probably something from the HttpUrlConnection or whatever it is called ... Personally, I use OkHttp and do not know the troubles) - YuriySPb
  • 2
    @Yuriy SPb I want to see how to work in Retrofit and OkHttp ... But by this moment I sort of understood how to solve my problem ... Now I’ll finish it and if I’ll publish it okay - Aleksey Timoshchenko
  • one
    You need to send a JSON string of 30 megabytes in size or a JSON string + some 30 Mb file. If the second option, you can make a PUT request to the server: json transfer the request header to the file in the request body. - Kirill Stoianov

2 answers 2

As a result, it was necessary to attach the HTTPUrlConnection itself to the header ...

That is, we open the stream for recording just as easily in the header we enclose everything we need as accompanying files.

  @Override public HttpURLConnection getHttpURLConnection(URL url, String newValue, Context context) { JSONObject service = getJsonObject(context); HttpURLConnection urlConnection = null; try { urlConnection = (HttpURLConnection) url.openConnection(); if (urlConnection != null) { urlConnection.setRequestProperty("authorization", UtilClass.getAuthToken(context)); urlConnection.setRequestProperty("service", service.toString()); urlConnection.setRequestMethod("POST"); urlConnection.setRequestProperty("Content-Type", newValue); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); urlConnection.setConnectTimeout(10000); urlConnection.connect(); } } catch (IOException e) { e.printStackTrace(); } return urlConnection; } 
  • So if a satisfactory answer is found, maybe the contest should be canceled so that the question is no longer displayed on the main page? - m. vokhm
  • @ m.vokhm In the same place, when you create a contest, you specify an additional comment in which I indicated that someone can have a different approach ... I just worked very little with competitors and maybe someone wants to state his vision) - Aleksey Timoshchenko
  • OK. I did not understand. I'm not special, but maybe somehow break this JSON into parts, and send it in parts? - m. vokhm
  • @ m.vokhm Maybe ... why? - Aleksey Timoshchenko

If you use Retrofit2, you can send it through it. Make a file with json. And something like this will look like the method of sending

 public interface FileUploadService { @Multipart @POST("upload") Call<ResponseBody> upload(@Part("description") RequestBody description, @Part MultipartBody.Part file); } private void uploadFile(Uri fileUri) { // create upload service client FileUploadService service = ServiceGenerator.createService(FileUploadService.class); // https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java // use the FileUtils to get the actual file by uri File file = FileUtils.getFile(this, fileUri); // create RequestBody instance from file RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file); // MultipartBody.Part is used to send also the actual file name MultipartBody.Part body = MultipartBody.Part.createFormData("picture", file.getName(), requestFile); // add another part within the multipart request String descriptionString = "hello, this is description speaking"; RequestBody description = RequestBody.create( MediaType.parse("multipart/form-data"), descriptionString); // finally, execute the request Call<ResponseBody> call = service.upload(description, body); call.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { Log.v("Upload", "success"); } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { Log.e("Upload error:", t.getMessage()); } }); } 

That's actually where everything is painted in detail:

Example of loading a file on the server