Search code examples
androidurlunknown-host

Sometimes my app crashes with UnknownHostException


In my app i have access to web server. It works fine in Some phones but while testing in Samsung Galaxy Model No - GT-S5830i Android Version - 2.3.6 it keeps on showing Unknown host exception. I have checked the url from browser its working fine.

private void submitUploadData(String url ,Map<String, String> param) throws IOException 
{  
    URL siteUrl;
    try {
        siteUrl = new URL(url); 
    HttpURLConnection conn = (HttpURLConnection) siteUrl.openConnection();
    conn.setRequestMethod("POST");
    conn.setDoOutput(true);
    conn.setDoInput(true);

    DataOutputStream out = new DataOutputStream(conn.getOutputStream());

    Set getkey = param.keySet();
    Iterator keyIter = getkey.iterator();
    String content = "";
    for(int i=0; keyIter.hasNext(); i++) {
        Object key = keyIter.next();
        if(i!=0) {
            content += "&";
        }
        content += key + "=" +  param.get(key);
    } 
    out.writeBytes(content.trim()); 
    out.flush();
    out.close();
    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
    String line = "";
    while((line=in.readLine())!=null) {
        System.out.println(line);
    }
    in.close();  
    //Intent home = new Intent(SyncHttp.this,TimeAndExpensesSKActivity.class);
    //startActivity(home);
    db.updateTimeExported(1);
    db.updateExpensesExported(1);
    db.close();
    db.close(); 

    if(db.getLogCount()==0){ 
            db.insertSyncDateDetails(getDateandTime());}
            else{
            db.updateSyncDateDetails(1, getDateandTime());}  


    } catch (MalformedURLException e) { 
        e.printStackTrace();
    }
    this.setResult(FINISH_RESULT);
    db.close();
}

I have already added permissions

 <uses-permission android:name="android.permission.INTERNET" />
 <uses-permission android:name="android.permission.CAMERA" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 <uses-permission android:name="android.permission.READ_PHONE_STATE"/> 

I am really confused why this exception occurring.

Thanks for your help guys.


Solution

  • My app did this too. This usually caused by unstable network connection. What I did was catching this UnknownHostException and modify the code in a way that if UnknownHostException happened, I try to re-fetch the URL again, after several miliseconds sleep.

    The basic algorithm is something like this:

    private void submitUploadData(String url ,Map<String, String> param) throws IOException 
    {  
        URL siteUrl;
        boolean isDataSubmitted = false;
        while(!isDataSubmitted){
            try {
                //do your complicated http process
                isDataSubmitted = true;
            } catch(UnknownHostException uhe){
                e.printStackTrace();
            } catch (MalformedURLException e) { 
                e.printStackTrace();
            }
        }
    }