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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
| package com.example.rech_exploi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.rech_exploi.MonAdapteur.MonAdapteurListener;
public class MainActivity extends ListActivity implements MonAdapteurListener{
Button btnRechercher;
TextView txtMsg;
EditText edtMsg;
JSONObject json_data=null;
HttpResponse response=null;
//ArrayList<String> donnees = new ArrayList<String>();
ArrayList<Exploitation> exploitations = new ArrayList<Exploitation>();
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressDialog = new ProgressDialog(MainActivity.this);
btnRechercher = (Button)findViewById(R.id.btnRechercher);
edtMsg = (EditText) findViewById(R.id.edtMsg);
}
public void onClickNom(Exploitation item, final int position) {
android.app.AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.icon);
builder.setTitle("Exploitation");
builder.setMessage("Vous avez cliqué sur : " + item.nom);
builder.setNegativeButton("Non", null);
builder.setPositiveButton("Oui", null);
/*builder.setPositiveButton("Oui",new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
Toast.makeText(this,"id"+exploitations.get(position).id, Toast.LENGTH_SHORT).show();
}
}
);*/
builder.show();
}
public void Rechercher(View v){
new asynchtask().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class asynchtask extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute() {
super.onPreExecute();
exploitations.clear();
progressDialog.setMessage("Chargement en cours");
progressDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
StringBuffer sb = new StringBuffer("");
BufferedReader br = null;
try{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://10.0.2.2/webService/Recherche.php");
String CIN = edtMsg.getText().toString();
if(CIN.length() > 0){
try{
List<NameValuePair> donnees = new ArrayList<NameValuePair>(1);
donnees.add(new BasicNameValuePair("message", CIN));
post.setEntity(new UrlEncodedFormEntity(donnees));
response = client.execute(post);
//edtMsg.setText("");
//Toast.makeText(MainActivity.this, "CIN envoyé!", Toast.LENGTH_SHORT).show();
Log.e("", "CIN envoye");
}
catch(ClientProtocolException e){
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
}
}
else
Toast.makeText(MainActivity.this,"Ce champ ne peut etre vide!", Toast.LENGTH_SHORT).show();
InputStream is = response.getEntity().getContent();
br = new BufferedReader(new InputStreamReader(is));
String ligneLue = br.readLine();
while(ligneLue !=null){
sb.append(ligneLue);
sb.append("\n");
ligneLue = br.readLine();
}
//Toast.makeText(MainActivity.this, sb+" result", Toast.LENGTH_SHORT).show();
}
catch(Exception e){
e.printStackTrace();
//Log.e("exception", e.getMessage());
Toast.makeText(MainActivity.this, "une erreur est survenue"+e.getMessage(), Toast.LENGTH_SHORT).show();
}
finally{
if(br !=null){
try{
br.close();
}catch(IOException e){
e.printStackTrace();
Log.e("exception", e.getMessage());
Toast.makeText(MainActivity.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++)
{
json_data = jArray.getJSONObject(i);
//donnees.add(json_data.getString("CIN"));
//donnees.add(json_data.getString("exploit"));
exploitations.add(new Exploitation(json_data.getInt("id"),json_data.getString("Nom"), json_data.getString("adr")));
}
}
catch(JSONException e){
Log.i("tagjsonexp",""+e.toString());
} catch (ParseException e) {
Log.i("tagjsonpars",""+e.toString());
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
ListView list = (ListView) MainActivity.this.findViewById(android.R.id.list);
MonAdapteur adpt = new MonAdapteur(getApplicationContext(),exploitations);
adpt.addListener(MainActivity.this);
list.setAdapter(adpt);
//setListAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_expandable_list_item_1, donnees));
edtMsg.setText("");
super.onPostExecute(result);
Log.e("asynchtask", "fin");
}
}
} |