lundi 4 mai 2015

Android HttpURLConnection only works with wifi and not with 3g or other mobile connection

i'm using HttpURLConnection for connect to my web server and download a compressed text with zlib and work perfectly when the device is connected to wifi but when i turn off the wifi and the app starts to work with 3g or any other connection, the app never can communicate with the server and the logCat throws differents types of errors.

Thread hilo1 = new Thread(new Runnable() {
                public void run() {
                    if (thereIsInternet()) {
                        boolean flag = true;
                        String result = new String();
                        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                        nameValuePairs.add(new BasicNameValuePair(
                                "version", version_insert));
                        nameValuePairs.add(new BasicNameValuePair(
                                "version_delete", version_delete));
                        nameValuePairs.add(new BasicNameValuePair(
                                "version_update", version_update));
                        nameValuePairs.add(new BasicNameValuePair("date",
                                date_last_update));
                        nameValuePairs.add(new BasicNameValuePair("version_banco",
                                version_banco));
                        InputStream is = null;
                        URL url=null;
                        try {
                            url = new URL(url_page);
                        } catch (MalformedURLException e1) {
                            // TODO Auto-generated catch block
                            flag = false;
                            e1.printStackTrace();
                        }
                        HttpURLConnection conn=null;
                        try {
                            conn = (HttpURLConnection) url.openConnection();
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            flag = false;
                            e1.printStackTrace();
                        }
                        conn.setReadTimeout(READ_TIMEOUT_CONEXION);
                        conn.setConnectTimeout(TIMEOUT_CONEXION);
                        HttpURLConnection.setFollowRedirects(true);
                        conn.setRequestProperty("accept", "*/*");  
                        conn.setRequestProperty("connection", "close");  
                        conn.setRequestProperty("User-Agent","");
                        //System.setProperty("http.keepAlive", "false");
                        try {
                            conn.setRequestMethod("POST");
                        } catch (ProtocolException e1) {
                            // TODO Auto-generated catch block
                            flag = false;
                            e1.printStackTrace();
                        }
                        conn.setDoInput(true);
                        conn.setDoOutput(true);

                        OutputStream os = null;
                        try {
                            os = conn.getOutputStream();
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            flag = false;
                            e1.printStackTrace();
                        }
                        BufferedWriter writer = null;
                        try {
                            writer = new BufferedWriter(
                                    new OutputStreamWriter(os, "iso-8859-1"));
                        } catch (UnsupportedEncodingException e1) {
                            // TODO Auto-generated catch block
                            flag = false;
                            e1.printStackTrace();

                        } 
                        try {
                            writer.write(getQuery(nameValuePairs));
                        } catch (UnsupportedEncodingException e1) {
                            // TODO Auto-generated catch block
                            flag = false;
                            e1.printStackTrace();
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            flag = false;
                            e1.printStackTrace();
                        }
                        try {
                            writer.flush();
                            writer.close();
                            os.close();
                            conn.connect();
                            int response = conn.getResponseCode();
                            is = conn.getInputStream();
                            byte[] uncompressedData = new byte[1024]; // where to store the data
                            // read the data
                            InputStream stream = new InflaterInputStream(is); 
                            // read data - note: may not read fully (or evenly), read from stream until len==0
                            int len, offset = 0;
                            String result1="";
                            while ((len = stream.read(uncompressedData , 0, uncompressedData .length))>0) {
                                //offset += len;
                                result1 = new String(uncompressedData, 0, len, "iso-8859-1");
                                result+=result1;
                               // Log.d("result ciclo ", "resultado : "+result1);
                            }           
                            stream.close();

                            if (is != null) {
                                is.close();
                            } 
                            conn.disconnect();
                        } catch (IOException e1) {
                            // TODO Auto-generated catch block
                            flag = false;
                            e1.printStackTrace();
                        } catch (HttpException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                            try {
                                JSONArray jArray = new JSONArray(result);
                                update_app=jArray.getString(12);
                                if (update_app.equals("1")){
                                    final String url_apk=jArray.getString(13);
                                    final String msg_user= jArray.getString(14);
                                    Inicio.this.runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                    Builder builder = new AlertDialog.Builder(Inicio.this);
                                    builder.setMessage(msg_user).setTitle(R.string.venta_info).setPositiveButton(R.string.boton_aceptar, new DialogInterface.OnClickListener() {
                                           public void onClick(DialogInterface dialog, int id) {
                                               Intent intent = new Intent(Intent.ACTION_VIEW ,Uri.parse(url_apk));
                                                startActivity(intent); 
                                           }
                                       });
                                    builder.create().show();
                                        }
                                    });
                                }else{
                                    new DownloadInfo().sincroDB(myDB, jArray, date_last_update, 0, 0);
                                }

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

Error 1
04-30 10:20:08.030: W/System.err(28055): java.net.SocketTimeoutException: failed to connect to http://ift.tt/1Pj6TTN (port 80) after 30000ms 04-30 10:20:08.045: W/System.err(28055): at libcore.io.IoBridge.connectErrno(IoBridge.java:159) 04-30 10:20:08.045: W/System.err(28055): at libcore.io.IoBridge.connect(IoBridge.java:112) 04-30 10:20:08.045: W/System.err(28055): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) 04-30 10:20:08.045: W/System.err(28055): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459) 04-30 10:20:08.045: W/System.err(28055): at java.net.Socket.connect(Socket.java:842) 04-30 10:20:08.045: W/System.err(28055): at libcore.net.http.HttpConnection.(HttpConnection.java:76) 04-30 10:20:08.045: W/System.err(28055): at libcore.net.http.HttpConnection.(HttpConnection.java:50) 04-30 10:20:08.045: W/System.err(28055): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340) 04-30 10:20:08.050: W/System.err(28055): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87) 04-30 10:20:08.050: W/System.err(28055): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128) 04-30 10:20:08.050: W/System.err(28055): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316) 04-30 10:20:08.050: W/System.err(28055): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311) 04-30 10:20:08.050: W/System.err(28055): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290) 04-30 10:20:08.050: W/System.err(28055): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240) 04-30 10:20:08.050: W/System.err(28055): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)

Error 2
05-04 12:09:05.346: W/System.err(4328): java.io.EOFException 05-04 12:09:05.376: W/System.err(4328): at libcore.io.Streams.readAsciiLine(Streams.java:203) 05-04 12:09:05.376: W/System.err(4328): at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:579) 05-04 12:09:05.376: W/System.err(4328): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:827) 05-04 12:09:05.376: W/System.err(4328): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283) 05-04 12:09:05.381: W/System.err(4328): at libcore.net.http.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497) 05-04 12:09:05.381: W/System.err(4328): at java.lang.Thread.run(Thread.java:841)

Error 3
04-30 10:32:32.450: W/System.err(29382): java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer) 04-30 10:32:32.465: W/System.err(29382): at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:545) 04-30 10:32:32.465: W/System.err(29382): at libcore.io.IoBridge.recvfrom(IoBridge.java:509) 04-30 10:32:32.465: W/System.err(29382): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488) 04-30 10:32:32.465: W/System.err(29382): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46) 04-30 10:32:32.465: W/System.err(29382): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240) 04-30 10:32:32.465: W/System.err(29382): at java.io.InputStream.read(InputStream.java:163) 04-30 10:32:32.465: W/System.err(29382): at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:142) 04-30 10:32:32.465: W/System.err(29382): at java.io.BufferedInputStream.read(BufferedInputStream.java:227) 04-30 10:32:32.465: W/System.err(29382): at libcore.io.Streams.readAsciiLine(Streams.java:201) 04-30 10:32:32.465: W/System.err(29382): at libcore.net.http.HttpEngine.readResponseHeaders(HttpEngine.java:579) 04-30 10:32:32.465: W/System.err(29382): at libcore.net.http.HttpEngine.readResponse(HttpEngine.java:827) 04-30 10:32:32.465: W/System.err(29382): at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:283) 04-30 10:32:32.470: W/System.err(29382): at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)

thanks for advance!

Aucun commentaire:

Enregistrer un commentaire