PDA

View Full Version : [Android] Inviare una richiesta http


Alhazred
25-02-2012, 02:01
Nella mia app (la prima che realizzo) devo inviare una richiesta http ad uno script php che ho sul mio sito.
Assieme alla richiesta devo mandare dei parametri tramite post.

Lo sto facendo così

public void postData(String from, String to, String date, String time, String searchby, String type, String orderby) {

// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.miosito.it/android/provahttp.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();
}
}

Vado però sempre a finire nell'ultimo catch IOException.

Cosa sto sbagliando?