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 98 99 100 101 102 103 104 105 106 107 108 109 110
|
public class maclasse extends ActionBarActivity {
ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
HttpURLConnection urlConnection = null;
ListView spp;
Button button3;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_societe);
spp=(ListView)findViewById(R.id.spinner66);
adapter=new ArrayAdapter<String>(this,R.layout.spin_layout,R.id.text,listItems);
spp.setAdapter(adapter);
button3=(Button)findViewById(R.id.button10);
spp.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
int position = arg2;
String message2 = (String) spp.getItemAtPosition(position);
Intent intent = new Intent();
int id=intent.getIntExtra("id", 1);
intent.putExtra("MESSAGE",message2);
setResult(2,intent);
finish();
}
});
}
public void onStart(){
super.onStart();
BackTask bt=new BackTask();
bt.execute();
}
private class BackTask extends AsyncTask<Void,Void,Void> {
ArrayList<String> list;
protected void onPreExecute(){
super.onPreExecute();
list=new ArrayList<String>();
}
protected Void doInBackground(Void...params){
InputStream is=null;
String result="";
try{
URL url = new URL("http://nom_de_domaine/fichier");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
is = urlConnection.getInputStream();
}catch(IOException e){
e.printStackTrace();
}
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"utf-8"));
String line = null;
while ((line = reader.readLine()) != null) {
result+=line;
}
is.close();
//result=sb.toString();
}catch(Exception e){
e.printStackTrace();
}
// parse json data
try{
JSONArray jArray =new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject jsonObject=jArray.getJSONObject(i);
// add interviewee name to arraylist
list.add(jsonObject.getString("nom"));
}
}
catch(JSONException e){
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result){
listItems.addAll(list);
adapter.notifyDataSetChanged();
}
}
} |
Partager