I have a problem I need to receive the memory stream from a webservice that was developed in C #. The routine below worked correctly, but now it falls into catch and the exception is null. What could be happening? is there a better way to receive a memory stream from a string?
Note: It goes to catch when it arrives on the line below:
InputStream input = conection.getInputStream();//urlx.openStream();conection.getInputStream();
@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.setConnectTimeout(300000);
conection.setReadTimeout(300000);
conection.connect();
// download do arquivoe
//InputStream input = new BufferedInputStream(urlx.openStream(),
// 8192);
InputStream input = conection.getInputStream();//urlx.openStream();conection.getInputStream();
String caminho = Environment.getExternalStorageDirectory().toString()+"/os.json";
try{
File f = new File (caminho);
f.delete();
} catch(Exception e){
Log.i("HDEBUG","Arquivo ainda não existe.");
}
// Output stream
FileOutputStream output = new FileOutputStream(caminho);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
// Escrevendo informação no arquivo
output.write(data, 0, count);
}
// flush no arquivo
output.flush();
// fechando streams
output.close();
input.close();
String linha = "";
// try{
// JSONArray arquivos = new JSONArray(obj.getString("webservice"));
// String caminho = Environment.getExternalStorageDirectory()+"/alvomobile/os_req/Lista1.json";
/*for(int j = 0; j < arquivos.length(); j++) {
JSONObject objx = arquivos.getJSONObject(j);
caminho = objx.getString("caminho");
}*/
BufferedReader arquivo = new BufferedReader(new FileReader(caminho));
int contador = 0;
while(arquivo.ready()) {
// pega a linha
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) {
// t.printStackTrace();
resposta.put("ok",false);
resposta.put("resposta","");
}
return resposta;
}
LogCat
12-05 14: 11: 43.758: E / Watchdog (213):! @Sync 633 12-05 14: 11: 54,313: D / STATUSBAR-NetworkController (301): onReceive () - RSSI_CHANGED_ACTION, WIFI_STATE, NETWORK_STATE 12-05 14: 11: 59,993: E / AlarmManagerService (213): android_server_AlarmManagerService_set to type = 3, 19316.488000000 12-05 14: 12: 00,000: E / AlarmManagerService (213): android_server_AlarmManagerService_set to type = 3, 19192.195000000 12-05 14: 12: 00,000: D / KeyguardUpdateMonitor (213): received broadcast android.intent.action.TIME_TICK 12-05 14: 12: 00,000: V / AlarmManager (213): ClockReceiver onReceive () ACTION_TIME_TICK 12-05 14: 12: 00.008: D / KeyguardUpdateMonitor (213): handleTimeUpdate 12-05 14: 12: 00.344: D / STATUSBAR-NetworkController (301): onReceive () - RSSI_CHANGED_ACTION, WIFI_STATE, NETWORK_STATE 12-05 14: 12: 03.360: D / STATUSBAR-NetworkController (301): onReceive () - RSSI_CHANGED_ACTION, WIFI_STATE, NETWORK_STATE 12-05 14: 12: 06.375: D / STATUSBAR-NetworkController (301): onReceive () - RSSI_CHANGED_ACTION, WIFI_STATE, NETWORK_STATE 12-05 14: 12: 13,758: E / Watchdog (213):! @Sync 634