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
| package com.example.ouste4863.jsonsuite;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import org.apache.http.HttpEntity;
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.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
/*int id= (int) jArray.getJSONObject();
String nom= (String) jArray.get(2);
String type= (String) jArray.get(3);
double prix= (double) jArray.get(4);*/
TextView ident= null;
TextView name= null;
TextView types= null;
TextView pri= null;
EditText editext;
static String marque;
// ident.setText(String.valueOf(id));name.setText(nom);types.setText(type);pri.setText(String.valueOf(prix));*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
types= (TextView) findViewById(R.id.type);
name=(TextView) findViewById(R.id.nom);
pri=(TextView) findViewById(R.id.Prix);
ident=(TextView) findViewById(R.id.identifiant);
Intent inte =getIntent();
marque=inte.getStringExtra("edit");
new Charge().execute();
}
public class Charge extends AsyncTask<String, String, String>{
InputStream is = null; String result = ""; String error_text=""; JSONObject j = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... urls) {
try {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
// nameValuePairs.add(new BasicNameValuePair("name","Apple"));
nameValuePairs.add(new BasicNameValuePair("name",marque));
// Envoie de la commande http
HttpResponse response=null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.43.218/android/fichier.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
//InputStream input = new BufferedInputStream(url.openStream(), 8192);
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
//while ((count = input.read(data)) != -1) {
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error in http connection " + e);
}
} catch (Exception e) {
// TODO: handle exception
}
return result;
}
@Override
protected void onPostExecute(String result) {
try{
String ch="";
//dismissDialog(progress_bar_type);
Log.e("log_tag 1", "retourn Affiche " + result );
if(!result.equals("")) {
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag 2","id: "+json_data.getString("ID")+
", name: "+json_data.getString("NAME")+
", type: "+json_data.getString("TYPE")+
", price: "+json_data.getString("PRICE")
);
types.setText( " type: "+json_data.getString("TYPE"));
pri.setText("price: "+json_data.getString("PRICE"));
ident.setText("id: "+json_data.getString("ID"));
name.setText( "name: "+json_data.getString("NAME"));
}
}
}catch(Exception e){
Log.e("log_tag", "Error parsing data " + e.toString());
}
}
}
} |
Partager