Throwable error returns null

Asked

Viewed 64 times

0

I have a problem I need to receive the memory stream of a webservice that was developed in C#. The following routine worked correctly, but now it keeps falling in the catch and the exception comes null. What could be happening? there is a better way to receive a memory stream from a string?

Note: It goes for the catch when it reaches 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

  • 1

    You can post what returns in your logcat?

  • Just a moment... I’ll be right back.

  • 1

    When you use this stretch } catch(Exception e){&#xA; Log.i("HDEBUG","Arquivo ainda não existe."); How do you know the file doesn’t exist? It could be any error.

  • In this location, he tries to remove the file... If there is any problem is that it does not exist, because I will recreate it understood. So I don’t need to do anything in catch.

  • @sicachester I edited and put the logcat msg...

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.