如何将数据库表中的所有数据(所有列)检索到android
任何有用的链接或任何youtube视频,这是php代码,我想用Android检索(成员)表中的所有数据,我想要android code,谢谢您的帮助和任何指南。
$your_querry="select * from androidlogin ";
$query=mysql_query($your_querry);
$list=array();
while($row=mysql_fetch_assoc($query))
{
$list[]=$row;
}
echo json_encode($list);发布于 2013-08-16 11:57:59
假设数据库中有CITY_ID、CITY_NAME等列。
public class MainActivity extends Activity {
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.enableDefaults();
tv=(TextView)findViewById(R.id.tv);
getData();
}
public void getData(){
String result= "";
InputStream isr= null;
try{
HttpClient httpclient=new DefaultHttpClient();
HttpPost httppost= new HttpPost("http://your_ip_address/php_filename.php");// your ip address and php file name here
HttpResponse response=httpclient.execute(httppost);
HttpEntity entity= response.getEntity();
isr=entity.getContent();
}
catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
tv.setText("Couldnt connect to database");
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(isr,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
isr.close();
result=sb.toString();
}
catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try {
String s = "";
JSONArray jArray = new JSONArray(result);
for(int i=0; i<jArray.length();i++){
JSONObject json = jArray.getJSONObject(i);
s = s +
"ID : "+json.getString("CITY_ID") +"\n"+
"NAME : "+json.getString("CITY_NAME")+"\n\n";
}
tv.setText(s);
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error Parsing Data "+e.toString());
}
}
}也要确保服务器在线!
https://stackoverflow.com/questions/18272451
复制相似问题