Click here to Skip to main content
15,884,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to retrieve the data from mysql and display it in a listView.
I am a beginner I try to retrieve only the data but I received nothing in emelateur.
thank you for helping me solve this problem.


tierss.php
mysql_connect("127.0.0.1","root","");
mysql_select_db("gestresor");
$q=mysql_query("SELECT * FROM Tiers");
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print(json_encode($output));
mysql_close();
?>


tierss.java
package com.example.login;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
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.JSONException;
import org.json.JSONObject;
public class Tierss extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_tierss);
String result = "";
//the year data to send
ArrayList<namevaluepair> nameValuePairs = new ArrayList<namevaluepair>();
//nameValuePairs.add(new BasicNameValuePair("year","1980"));
InputStream is=null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://10.0.2.2/formation/tierss.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jarray.length();i++){>
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","id: "+json_data.getInt("Code_Tiers")+", name: "+json_data.getString("Libelle_Tiers")+", tel: "+json_data.getString("Num_Tel_Tiers")+", type: "+json_data.getString("Type_Tiers")+", adresse: "+json_data.getString("Adresse_Tiers")+", code postal: "+json_data.getString("Code_Postal")+", ville: "+json_data.getInt("ville"));
}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
} }
Posted
Comments
Ganesh KP 7-Sep-13 2:44am    
Hi ingenis, Are you getting any exception? Please post your Logcat and use pastie.org to post your log cat or any sort of code.
wseng 27-Aug-16 1:03am    
Kindly format your code before you post.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900