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 111 112 113 114 115 116
|
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.ListActivity;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class ListeTitres extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//setListAdapter(new CustomListetitre());
setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
new ArrayList<String>()));
new AjoutItemListe().execute();
}
public void onListItemClick(ListView parent, View v, int position, long id){
//selection.setText(items[position]);
}
class AjoutItemListe extends AsyncTask<Void, String, Void> {
@Override
protected Void doInBackground(Void... unused) {
try{
// Création du httpclient android acceptant le ssl
AndroidHttpClient httpclient = AndroidHttpClient.newInstance("Android");
HttpPost httpPost = new HttpPost("monURL.php");
// Préparation des parametres post
List<NameValuePair> parametres = new ArrayList<NameValuePair>();
parametres.add(new BasicNameValuePair("a", "3"));
// On encode les parametres et on les attache au httpPost (l'url)
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parametres);
httpPost.setEntity(formEntity);
//Execution de la requete + Récup de reponse...
HttpResponse httpResponse = httpclient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
if(httpEntity != null){
InputStream resultatPage = httpEntity.getContent();
// Lecture du retour au format json
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(resultatPage));
StringBuilder stockageReponse = new StringBuilder();
// Stockage de la réponse dans le stockageReponse
String lignelue = bufferedReader.readLine();
while(lignelue != null){
stockageReponse.append(lignelue + "\n");
lignelue = bufferedReader.readLine();
}
bufferedReader.close();
// Analyse de la reponse...
JSONObject jsonObject = new JSONObject(stockageReponse.toString());
// Recup info titre
JSONArray jsonMusique = jsonObject.getJSONArray("musique");
// Initialisation variable
int id_musique = 0;
String titre_musique = "";
String artiste = "";
String album = "";
for(int i=0; i<jsonMusique.length();i++){
id_musique = jsonMusique.getJSONObject(i).getInt("id");
titre_musique = jsonMusique.getJSONObject(i).getString("titre");
artiste = jsonMusique.getJSONObject(i).getString("artiste");
album = jsonMusique.getJSONObject(i).getString("album");
publishProgress(titre_musique);
}
}
httpclient.close();
} catch(Exception e){
Log.e("Erreur","Récup titre impossible :"+ e);
}
return(null);
}
@SuppressWarnings("unchecked")
@Override
protected void onProgressUpdate(String... item) {
((ArrayAdapter<String>)getListAdapter()).add(item[0]);
}
@Override
protected void onPostExecute(Void unused) {
Toast
.makeText(ListeTitres.this, "Chargement complet!", Toast.LENGTH_SHORT)
.show();
}
}
} |
Partager