Search code examples
androidandroid-asynctaskandroid-volleyandroidhttpclient

Best way to send large data to server in Android


I have zip file in my sdcard,size is 8-9 mb.I'm using this library to send data.

https://github.com/loopj/android-async-http

and this is my source

private  void sendSubmitRequest(File file)
{
    AsyncHttpClient client = new AsyncHttpClient();

    RequestParams params = new RequestParams();
    showpDialog("loading");

    try
    {

        params.put("vin", vinNumber.getText().toString());
        params.put("polygon", polygonAdapter.getItem(spinnerPosition).getId()+"");


        params.put("scanner_key", vinScannerModel.getSecretKey());
        params.put("status", status);
        params.put("user", vinScannerModel.getId()+"");

        InputStream inputStream = null;
        inputStream = new FileInputStream(file.getAbsolutePath());
        byte[] buffer = new byte[8192];
        int bytesRead;
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        Base64OutputStream output64 = new Base64OutputStream(output, Base64.DEFAULT);
        try {
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                output64.write(buffer, 0, bytesRead);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        output64.close();

        String attachedFile = output.toString();


        params.put("photos", attachedFile);

    }
    catch (Exception e)
    {
       e.printStackTrace();
    }


    client.post(ServerUtil.addVin, params, new TextHttpResponseHandler() {
        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            showOneItemDialog(throwable.toString(),false);
            hidepDialog();


            Log.d("server status",responseString+"");
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, String responseString) {
            Log.d("server status",responseString+"");
            try {
                hidepDialog();
                JSONObject mainJsonObject = new JSONObject(responseString.toString());
                boolean status=mainJsonObject.getBoolean("status");
                String desc=mainJsonObject.getString("desc");
                if(desc!=null)
                    showOneItemDialog(desc,status);
            } catch (JSONException e) {
                e.printStackTrace();
                showOneItemDialog(e.toString(),false);
            }
        }
    });




}

My code working perfect but i want to check if my solution is complete?


Solution

  • You will want to run this in an upload Service instead of as part of the Activity/UI. If the user kills the Activity (or if it's otherwise removed due to memory pressure), you'll lose the upload.

    I haven't used this particular library, but one like this seems like something you'll find useful and a quick glance shows it has most of the features one generally needs: https://github.com/gotev/android-upload-service