I am trying to load data from MySQL database to android ListView. This one is the example I saw : http://codeoncloud.blogspot.com/2013/07/android-mysql-php-json-tutorial.html
Here's my code :
public class AbCdEfGh extends ActionBarActivity {
private String jsonResult;
private String url = "http://merosong.byethost24.com/android_php_files_management/connection.php";
private ListView listView;
ProgressDialog pDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//showing a ProgressDialog
pDialog = new ProgressDialog(this);
pDialog = ProgressDialog.show(this, "", "Loading", true);
setContentView(R.layout.activity_choose);
listView = (ListView) findViewById(R.id.list_main);
accessWebService();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present
getMenuInflater().inflate(R.menu.menu_choose, menu);
return true;
}
// Async Task to access the web
private class JsonReadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
try {
HttpResponse response = httpclient.execute(httppost);
jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
} catch (IOException e) {
}
return answer;
}
@Override
protected void onPostExecute(String result) {
pDialog.dismiss();
ListDrwaer();
}
}// end async task
public void accessWebService() {
JsonReadTask task = new JsonReadTask();
// passes values for the urls string array
task.execute(new String[]{url});
}
// build hash set for list view
public void ListDrwaer() {
List<Map<String, String>> trackList = new ArrayList<Map<String, String>>();
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("mstracks");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("name");
String outPut = name + "";
trackList.add(createEmployee("merosongayush", outPut));
}
} catch (JSONException e) {
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this, trackList,
android.R.layout.simple_list_item_1,
new String[]{"merosongayush"}, new int[]{android.R.id.text1});
listView.setAdapter(simpleAdapter);
}
private HashMap<String, String> createEmployee(String name, String link) {
HashMap<String, String> finalNameLink = new HashMap<String, String>();
finalNameLink.put(name, link);
return finalNameLink;
}
}
I'm getting nothing in my android activity.
My connection.php output :
http://merosong.byethost24.com/android_php_files_management/connection.php
Please Help. Thanks in advance.