• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps httprequest fails

Kumidan

Newbie
I'm trying to send an http request from my app to script on my website.
The script simply writes the parameters received from the request.
If I send the request from a browser it works, so the script is ok, but from my app nothing happens.

This is the code I'm using, do you see anything wrong in it?
The toast at the beginning is only to check if the parameters arrive correctly to the function, they are correct.
Code:
public void postData(String from, String to, String date, String time, String searchby, String type, String orderby) {
        
        String testo = from+" - "+to+" - "+date+" - "+time+" - "+searchby+" - "+type+" - "+orderby; 
        Toast toast=Toast.makeText(this, testo, 6000);
        toast.setGravity(Gravity.TOP, -30, 50);
        toast.show();
        
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://www.mysite.it/android/testhttp.php");

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("from", from));
        nameValuePairs.add(new BasicNameValuePair("to", to));
        nameValuePairs.add(new BasicNameValuePair("date", date));
        nameValuePairs.add(new BasicNameValuePair("time", time));
        nameValuePairs.add(new BasicNameValuePair("searchby", searchby));
        nameValuePairs.add(new BasicNameValuePair("type", type));
        nameValuePairs.add(new BasicNameValuePair("orderby", orderby));
        
        // Url Encoding the POST parameters
        try {
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        } catch (UnsupportedEncodingException e) {
            // writing error to Log
            e.printStackTrace();
        }
 
        // Making HTTP Request
        try {
            HttpResponse response = httpclient.execute(httppost);
 
            // writing response to log
            Log.d("Http Response:", response.toString());
        } catch (ClientProtocolException e) {
            // writing exception to log
            e.printStackTrace();
        } catch (IOException e) {
            // writing exception to log
            e.printStackTrace();
 
        }
    }
 
Debugging the code I've found that the app goes into the catch

catch (IOException e) {
// writing exception to log
e.printStackTrace();
}

that should means that the server (my site) doesn't reply correctly (???)
What can I try to solve this?
 
Back
Top Bottom