データベースに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());
}
}
}
また、必ずサーバーをオンラインにしてください。