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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class Reception extends ActionBarActivity {
Button btn = null;
Spinner sp = null;
List lst=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
sp = (Spinner) findViewById(R.id.sp);
lst=new ArrayList<String>();
}
public class ali extends AsyncTask<Void, Void, Void>{
StringBuffer sb = new StringBuffer("");
BufferedReader br = null;
@Override
protected Void doInBackground(Void... arg0) {
try {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet();
URI uri = new URI("http://10.0.2.2/Projets/reception.php");
get.setURI(uri);
HttpResponse rep = client.execute(get);
InputStream is = rep.getEntity().getContent();
br = new BufferedReader(new InputStreamReader(is));
String lignlu = br.readLine();
while (lignlu != null) {
sb.append(lignlu);
sb.append("\n");
lignlu = br.readLine();
}
} catch (Exception E) {
Toast.makeText(Reception.this, "Une erreur est survenue",
Toast.LENGTH_SHORT).show();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
Toast.makeText(Reception.this,
"Une erreur grave est survenue", Toast.LENGTH_SHORT)
.show();
}
}
}
try {
JSONArray jArray = new JSONArray(sb.toString());
for(int i=0;i<jArray.length();i++){
lst.add(jArray.getJSONObject(i).getString("login")
.toString());}
ArrayAdapter<String> adapt = new ArrayAdapter<>(Reception.this,
android.R.layout.simple_spinner_item, lst);
sp.setAdapter(adapt);
btn.setEnabled(false);
} catch (JSONException je) {
Toast.makeText(Reception.this,
"Une erreur trés grave est survenue", Toast.LENGTH_SHORT)
.show();
}
return null;
}
}
} |
Partager