1
I am trying to download a JSON file, it is sent to me via Multipart, I can recover it but I don’t want to save it in a physical file, so I am taking the return and converting it into string. My problem is in the conversion because it brings me the multi part headers so I can’t possibly convert it to a json object. Has how I remove these header and stay only with the body of the answer without the need to record the file ?
below is an example of the string response.
--e237ecf6-b3ab-4eb0-b94a-8077a7abb566
Content-Disposition: form-data; name=Lists; filename=Lists
[{"Code company":"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;
}
instead of multi.getSubType(); I would use multi.getBodyPart(); ?
– Hiago Souza
@Hiagosouza Yes, but the difference is I wouldn’t return one
String
.– utluiz
So more here in case I only have the getBodyParts method
– Hiago Souza
In fact I believe it is the way in which I am receiving this file, the method I am using is correct to receive Multipart ?
– Hiago Souza
@Hiagosouza I just realized we’re talking about different Apis. I rephrased the answer.
– utluiz
Didn’t work =/
– Hiago Souza
respws = new String(baos.toByteArray(),"UTF-8"); Multipart multi = new Multipart(respws); Bodypart body = (Bodypart) multi.getBodyParts(); Httpentity corpofinal = ((Httpresponse) body). getEntity(); reply.put("ok",true); reply.put("response", corpofinal.getContent());
– Hiago Souza
@Hiagosouza "Didn’t work" is very vague. I have already said that without seeing the contents of the message there is no way to be sure of the implementation. The battery of my crystal ball is over. Also, you’re trying to put the answer into a JSON object, but you’re not even checking what the
getContent
returned. Java will not convert content to Json automatically. It is probably a String and you will have to convert from String to Json.– utluiz
Okay, I meant you didn’t bring me any content. Otherwise it brings the content but with the Multipart information, if I save the file and open it again it works, however it is one more step and as I have a great content take longer. I’ll try again and return to you what happened.
– Hiago Souza