Redeem only the body of the MultiPart response

1

I'm trying to download a JSON file, it's sent to me via MultiPart, I can recover it however I do not want to save it to a physical file, so I'm getting the return and converting it to string. My problem is in the conversion because it brings me the headers of the multi part so I can not convert it to a json object. Is there a way to remove these header and get only the body of the response without the need to save the file?

Below is an example of the string response.

  

- e237ecf6-b3ab-4eb0-b94a-8077a7abb566

     

Content-Disposition: form-data; name = ListPrevias; filename = ListPrevias

     

[{"Company Code": "1", .........................]

Below is the method I'm using.

public static JSONObject downloadMultiPart(String url) throws JSONException {
    JSONObject resposta = new JSONObject();
    String respws;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        Log.i("HDEBUG","Download de JSON - URL ["+url+"]");

        HttpURLConnection con = (HttpURLConnection) ( new URL(url)).openConnection();
        con.setRequestMethod("GET");
        con.setConnectTimeout(30000); // 30 segundos de time out
        con.setDoInput(true);
        con.setDoOutput(false);
        con.connect();

        InputStream is = con.getInputStream();
        byte[] b = new byte[1024];

        while ( is.read(b) != -1)
            baos.write(b);

        con.disconnect();

        respws = new String(baos.toByteArray(),"UTF-8");

        Multipart multi = new Multipart(respws);
        String corpo = multi.getSubType();

        resposta.put("ok",true);
        resposta.put("resposta", corpo.toString());
    }
    catch(Throwable t) {
        resposta.put("ok",false);
        resposta.put("resposta","");
    }

    return resposta;
}
    
asked by anonymous 13.08.2014 / 16:38

2 answers

1

I have refaced everything and now it worked, follow below the code I hope will be useful to you.

@SuppressLint("NewApi") public static JSONObject downloadMultiPart(String url) throws JSONException {
    JSONObject resposta = new JSONObject();
    String respws;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {
        URL urlx = new URL(url);
        URLConnection conection = urlx.openConnection();
        conection.connect();

        InputStream input = new BufferedInputStream(urlx.openStream(),
                8192);

        String caminho = Environment.getExternalStorageDirectory().toString()+"/OsManager.kml";

        try{
            File f = new File (caminho);
            f.delete();
        } catch(Exception e){
            Log.i("HDEBUG","Arquivo ainda não existe.");
        }

       FileOutputStream output = new FileOutputStream(caminho);

        byte data[] = new byte[1024];

        long total = 0;

        int count;
        while ((count = input.read(data)) != -1) {
            total += count;
            output.write(data, 0, count);
        }

        output.flush();

        output.close();
        input.close();


        String linha = "";
        BufferedReader arquivo = new BufferedReader(new FileReader(caminho));
        int contador = 0;
        while(arquivo.ready()) {    
            String tmp_linha = arquivo.readLine();
            if(contador == 3)
            {
                linha = tmp_linha;
            }
            contador = contador + 1;
        }
        File filem = new File(caminho);
        filem.delete();

        resposta.put("ok", true);
        resposta.put("resposta", linha);
    }
    catch(Throwable t) {
        resposta.put("ok",false);
        resposta.put("resposta","");
    }

    return resposta;
}
    
13.08.2014 / 21:23
0

Instead of getSubType() , use the method getBodyParts() to retrieve a list that actually contains the body of the message. The content must be in one of the elements of the list, which in fact probably only has one element.

The method return will be BodyPart . Content can be accessed by the getEntity .

However, sometimes the body of a multipart message is another multipart message. Then it might be that you have to "go down" another level, that is, the object (entity) returned by getEntity can be another MultiPart .

If you have any questions, please post a copy of the full message so that you can review it.

    
13.08.2014 / 17:15