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;
}