0

Estou com um problema eu preciso receber o memory stream de um webservice que foi desenvolvido em C#. A rotina abaixo funcionava corretamente, porém agora ela fica caindo no catch e a exceção vem nula. Oque pode estar acontecendo? há uma maneira melhor de receber um memory stream de uma string?

Obs: Ele vai para o catch quando chega na linha abaixo:

    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

5
  • 1
    Pode postar o que retorna no seu logcat?
    – rsicarelli
    5/12/2014 às 14:29
  • só um momento... já vou rodar denovo a rotina e pego o retorno. 5/12/2014 às 15:59
  • 1
    Quando você usa este trecho } catch(Exception e){ Log.i("HDEBUG","Arquivo ainda não existe."); como você sabe que o arquivo não existe? Pode ser qualquer erro.
    – Maniero
    5/12/2014 às 16:12
  • Neste local, ele tenta remover o arquivo... Se der algum problema é que o mesmo não existe, pois eu vou recriar ele entendeu. Então eu não preciso fazer nada no catch. 5/12/2014 às 16:13
  • @sicachester editei e coloquei a msg do logcat... 5/12/2014 às 16:14

0

Você deve fazer log-in para responder a esta pergunta.

Pesquise outras perguntas com a tag .