Error httpUrlConnection, bufferedreader or inputStream

1

I have a asynctask that receives data from a remote file that I access through a java servlet. The fact is that when the task takes a little longer because you have to create the record in the bbdd, when I receive the inputStream with the data of the record created, with code 200, all correct, when it reaches the while to read the buffer , casca, without giving an error or Exception or anything, it leaves directly from the loop and returns an empty string because it has not filled in anything clear. I have tried to put the reading timeout and conection but nothing. If someone can help me perfect.

 public class ObtenerConfiguracion extends AsyncTask<Void, Void, String>{

    private HttpURLConnection con = null;
    private BufferedReader buffer = null;
    private StringBuffer response = null;
    private InputStreamReader miIS = null;
    private int responseCode;
    private String strResponseMsg;
    private ProgressDialog pDialog;

    Callback callback;

    public ObtenerConfiguracion(Callback callback){
        this.callback=callback;
    }

    @Override
    protected void onPreExecute(){
        super.onPreExecute();
        pDialog = new ProgressDialog(Inicio.this);
        pDialog.setMessage(getText(R.string.txtCargarConfig));
        pDialog.setIndeterminate(false);
        pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected String doInBackground(Void... params) {

        int apiSDK = Build.VERSION.SDK_INT;
        String apiStr = Integer.toString(apiSDK);

        URL url = null;
        try {
            url = new URL("https://XXXXXXXXXXXXXXXXXXXXX");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        try {
            // Construir los datos a enviar
            String data = "serial=" + URLEncoder.encode(Build.SERIAL,"UTF-8")+ "&device=" + URLEncoder.encode(Build.DEVICE,"UTF-8")
            + "&id=" + URLEncoder.encode(Build.ID,"UTF-8") + "&brand=" + URLEncoder.encode(Build.BRAND,"UTF-8")
            + "&model=" + URLEncoder.encode(Build.MODEL,"UTF-8") + "&api=" + URLEncoder.encode(apiStr,"UTF-8");

            Log.d("HTTP", "parametros POST: " + data);

            con = (HttpURLConnection)url.openConnection();
            con.setRequestMethod("POST");
            // Activar método POST
            con.setDoOutput(true);
            // Tamaño previamente conocido
            con.setFixedLengthStreamingMode(data.getBytes().length);
            // Establecer application/x-www-form-urlencoded debido a la simplicidad de los datos
            con.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

            Log.d("HTTP", "ponemos timeout");
            con.setConnectTimeout(20000);
            con.setReadTimeout(60000);

            con.connect();

            OutputStream out = new BufferedOutputStream(con.getOutputStream());

            out.write(data.getBytes());
            out.flush();
            out.close();

            responseCode = con.getResponseCode();
            Log.d("HTTP", "POST Response Code : " + responseCode);

            strResponseMsg="";

            if (responseCode == HttpURLConnection.HTTP_OK) { //success
                Log.d("HTTP", "Empezamos a leer inputStream");

                String inputLine = null;
                response = new StringBuffer();

                miIS= new InputStreamReader(con.getInputStream());
                buffer = new BufferedReader(miIS);

                //buffer= new BufferedReader(new InputStreamReader(con.getInputStream()));
                Log.d("HTTP", "inputS" + miIS.toString());
                Log.d("HTTP", "buffer" + buffer.toString());

                while ((inputLine = buffer.readLine()) != null) {
                    response.append(inputLine);
                }
                buffer.close();

                strResponseMsg = response.toString();

                Log.d("HTTP", strResponseMsg);

            } else {
                Log.d("HTTP", "Error en el POST: " + responseCode);
            }

        } catch (java.net.SocketTimeoutException e) {
            Log.d("HTTP", "Timeout exception");
            e.getMessage();
            e.printStackTrace();
        } catch (IOException e) {
            Log.d("HTTP", "Io exception");
            e.getMessage();
            e.printStackTrace();
        } catch (Exception e) {
            Log.d("HTTP", "Exception");
            e.getMessage();
            e.printStackTrace();
        } finally {
            if(con!=null)
                con.disconnect();
        }
        Log.d("HTTP", "hacemos return strmresponsemsg");
        return strResponseMsg;
    }

    @Override
    protected void onProgressUpdate(Void... values){

    }

    @Override
    protected void onPostExecute(String resultado){

        callback.run(resultado);
        pDialog.dismiss();

    }
}

Although it does not say anything, I pass the Logcat so you can see how it jumps directly to the return in the middle of a loop

  

03-31 18: 36: 28.309 10578-10792 I / OpenGLRenderer: Initialized EGL,   version 1.4 03-31 18: 36: 28.337 10578-10794 I / DpmTcmClient:   RegisterTcmMonitor from: com.android.okhttp.TcmIdleTimerMonitor 03-31   18: 36: 28.347 10578-10794 D / HTTP: we put timeout 03-31 18: 36: 36.147   10578-10794 D / HTTP: POST Response Code: 200 03-31 18: 36: 36.147   10578-10794 D / HTTP: We start reading inputStream 03-31 18: 36: 36.148   10578-10794 D / HTTP: inputSjava.io.InputStreamReader@1d74507 03-31   18: 36: 36.148 10578-10794 D / HTTP: bufferjava.io.BufferedReader@a1ab634   03-31 18: 36: 36.149 10578-10794 D / HTTP: we do return strmresponsemsg   03-31 18: 36: 36.150 10578-10578 D / callback: we do not recover POST data

    
asked by migaleroc 31.03.2017 в 19:01
source

0 answers