Question

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.

Was it helpful?

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();
        }
    }
}

OTHER TIPS

I have also experienced some issues using URLConnections especially HTTPS on the Samsung Galaxy TAB, but i would imagine it probably occurs on other models as well since there are a ton of devices with the same name but made for different markets and mobile providers.

So far i haven't found a way around sorry, since i tend to avoid the URLConnection class due to a ton of other issues it has.

I suggest using the Apache HTTP Client to communicate with your API's. you can google a ton of examples of using it.

Also when looking at your code i would suggest reading a bit more into the try-catch-finally block statements in java. it is really important to properly close connections at the end or in case of errors.

Other possibilities are:

  1. There is no connection of any sort active (mobile or wifi)

  2. You are missing the "http://www." in your url.

I think calling close() on the DataOutputStream before you recieve your input might close the whole connection - try commenting out that line.

Have you checked the URL from the phone's browser? Please log the URL you pass to the function and try it in your phone's browser. I would think of missing/incorrect protocol specifier and or URL string formation issues of which this particular phone is unforgiving.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top