Unknown host exception Android

1

My app consumes a web service every time it detects an entry to a Geofence, but sometimes the mobile data is off or is wrong because of the signal and when sending sends a UnknownHostException , but I would like it when I have it again data signal or Wifi send that data to the web service automatically. Is there any way to do it?

The connection I'm making with HttpUrLConnection :

        URL obj;
        obj = new URL(preURL);

        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("User-Agent", USER_AGENT);
        con.setRequestProperty("Accept-Charset", "UTF-8");
       con.setConnectTimeout(60*1*1000);
        int responseCode = con.getResponseCode();
        Log.d("responseCode", "responseCode -->" + responseCode);
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();

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

        if (response.toString() != null) {
            result = response.toString();
            Log.i(LOG_TAG, "RESPUESTA=" + result);
        }

    }catch (SocketTimeoutException e) {
        LogFile.createLogFile(e+utils.getCurrentDate(), this.getClass().getSimpleName() + "_" + e);
        Log.v("Exepcion", "Se agoto el tiempo");
        Log.e(LOG_TAG, "ERROR - (SocketTimeoutException) No Se logro enviar log...");
        Log.e(LOG_TAG, e.getMessage());
        e.printStackTrace();
        return "";

    } catch (MalformedURLException e) {
        LogFile.createLogFile(utils.getCurrentDate() + e , this.getClass().getSimpleName() + "_" + e);
        Log.e(LOG_TAG, "ERROR - (MalformedURLException) No Se logro enviar log...");
        Log.e(LOG_TAG, e.getMessage());
        e.printStackTrace();
        return "";
    } catch (ProtocolException e) {
        LogFile.createLogFile(utils.getCurrentDate() + e, this.getClass().getSimpleName() + "_" + e);
        Log.e(LOG_TAG, "ERROR - (ProtocolException) No Se logro enviar log...");
        Log.e(LOG_TAG, e.getMessage());
        e.printStackTrace();
        return "";
    } catch (IOException e) {
        LogFile.createLogFile(utils.getCurrentDate()+e, this.getClass().getSimpleName() + "_" + e);
        Log.e(LOG_TAG, "ERROR - (IOException) No Se logro enviar log...");
        //Log.e(LOG_TAG, e.getMessage());
        e.printStackTrace();
        return "";
    } finally {
        Log.e(LOG_TAG, "FINAL...");
    }
    
asked by AndresLeon 13.09.2017 в 05:17
source

2 answers

0

Your question is a bit long, so I'll summarize how I would do it:

  • Capturing the UnknowHostException would save that data in some local database, I recommend using realm , you can see the documentation here link It's quite simple to use.

  • In your object where you save data from the local database add a boolean status to see if it has already been synchronized or not

  • you can use this android broadcast to know when the connection status has changed and send the data to the database.

<receiver android:name=".TURECIBIDOR">
   <intent-filter>
      <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
   </intent-filter>
</receiver>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Another way a little simpler would be to leave a button to synchronize data in your Toolbar and send your realm object where the boolean estado = false . I also recommend using retrofit to get the webservice and its possible errors in a much more simple and efficient link seems extensive but actually you adapt fast

I hope you do well with everything.

    
answered by 13.09.2017 / 06:29
source
-1

Based on your message:

  

Unknown host exception

This error usually refers to Android that your url does not have a protocol defined and also is not valid, for example, these would be valid urls (assuming they are accessible):

http://www.andresleon.com/android
http://www.andresleon.com

but you are not:

www.andresleon.con/android
andresleon.con/android
jorgesysprueba.agencytoots.com

so when trying to use them you would get the error:

  

java.net.UnknownHostException    Unable to resolve host "jorgesysprueba.agencytoots.com": No address   associated with hostname

Ensure that the value of preURL actually has the protocol http:// or https:// and that the domain is valid! .

    
answered by 13.09.2017 в 17:30