0

I'm developing an Android app that gets a JSON_encoded result from a php middleware script that connects to a MySQL database. I have given the application Internet permissions. The problem I'm having is that the program gives an UnknownHostException error the first time it is run. I have the program on a timer, and subsequent calls to the timer handler function do not return the UnknownHostException error. Do you have any idea why this would occur? I have tested the domain and made sure that it connects correctly through a web browser. Here's a snippet from the code:

    public final void timerAlert(){ 

    final Handler handler = new Handler();
    final Runnable r = new Runnable()
    {
        public void run() {

            Timer_Method();
            handler.postDelayed(this,1000);
        }

    };
    handler.postDelayed(r, 1000);
}

public void Timer_Method()
{

    //See if this buzzer is being signaled.
    String result = null;
    InputStream is = null;
    StringBuilder sb=null;
    //http post
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("BuzzerID",BuzzerID.toString()));
   Toast.makeText(getBaseContext(), "BuzzerID="+BuzzerID.toString() ,Toast.LENGTH_LONG).show();
    try{
         HttpClient httpclient = new DefaultHttpClient();
         HttpPost httppost = new HttpPost("http://domain/getBuzzStatus.php?BuzzerID="+BuzzerID.toString());
         httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
         HttpResponse response = httpclient.execute(httppost);
         HttpEntity entity = response.getEntity();
         is = entity.getContent();

    }catch(Exception e){
             Log.e("log_tag", "Error in http connection"+e.toString());
           Toast.makeText(getBaseContext(), "Hm, problem here="+e.toString() ,Toast.LENGTH_LONG).show();
         }

Note that domain has something else there in the actual code and that this is just a snippet but is where the first issue occurs. Also note that I am mixing get and post, something I'd rather not do, but for some reason passing the nameValuePair to the php script doesn't send anything to $_REQUEST.

A snippet from the very simple PHP script:

$sql_string="SELECT Signal FROM BuzzCustomer WHERE idBuzzCustomer=" . $_GET['BuzzerID'];

$sql = mysql_query($sql_string);


while($row=mysql_fetch_assoc($sql))  
    $output[]=$row;  
print(json_encode($output));  

mysql_close();

I switched to $_GET here because I could not get $_REQUEST to work. Any help would be appreciated. Thanks in advance!

DispName
  • 5
  • 4

2 Answers2

1

May be their will be no Internet connection in the simulator......Check a url in browser

Instead of

HttpResponse response = httpclient.execute(httppost);
         HttpEntity entity = response.getEntity();
         is = entity.getContent();

Use

ResponseHandler<String>  response = new BasicResponseHandler();
String result = httpclient.execute(httppost,response);

Also put Internet permission in the Manifest

droid kid
  • 7,569
  • 2
  • 32
  • 37
  • I mentioned that I did check in a web browser to ensure it works. It does. Also, I'm using an actual Android phone, not the emulator. – DispName Apr 23 '11 at 13:03
0

Just a guess, but maybe the DNS request takes too long and your HttpClient gives up, but the request is finished and cached so the next time it does not fail?
This With Apache HttpClient, why isn't my connection timeout working? seems to be a question about how to set the timeout for HttpClient.

Community
  • 1
  • 1
Torp
  • 7,924
  • 1
  • 20
  • 18
  • Thanks! That might indeed be the issue. I was suspecting some sort of timeout issue, but was uncertain why it only fails on the first call. I'll try setting the timeout and see if that fixes the issue. – DispName Apr 23 '11 at 13:52
  • Looks like a lot of that page is now using deprecated methods. I'm still looking for the correct way to set the timeouts for the HttpClient. – DispName Apr 23 '11 at 14:16
  • Looks like this is the proper way to do it now: http://stackoverflow.com/questions/693997/how-to-set-httpresponse-timeout-for-android-in-java – DispName Apr 23 '11 at 14:19
  • Looks like it was a timeout issue. I'm not seeing any more burnt toast. – DispName Apr 23 '11 at 14:24