1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class ServiceConnexionSite extends Service {
private HttpClient client = null;
private HttpPost httppost = null;
private List<NameValuePair> pairs = null;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
client = new DefaultHttpClient();
httppost = new HttpPost("http://www.mondomaine.fr/monscript.php");
pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("key", "value"));
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
try {
httppost.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse reponse = client.execute(httppost);
Toast.makeText(this, "Connexion réussie", Toast.LENGTH_SHORT).show();
} catch(Exception e) {
Toast.makeText(this, "Connexion non établie", Toast.LENGTH_SHORT).show();
e.getMessage();
}
return super.onStartCommand(intent, flags, startId);
}
} |
Partager