I'm unable to retrieve the data from MySQL
database to the app. When I click on the button to show the data, the activity closes itself and goes to main activity.
public class Cohorts extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cohorts);
String result = null;
InputStream is = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://website/new/db_connect.php");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success");
// Toast.makeText(getApplicationContext(), “pass”, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection" + e.toString());
Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();
}
//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");
// Toast.makeText(getApplicationContext(), “Input Reading pass”, Toast.LENGTH_SHORT).show();
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result" + e.toString());
Toast.makeText(getApplicationContext(), "Input reading fail", Toast.LENGTH_SHORT).show();
}
//parse json data
try {
JSONArray jArray = new JSONArray(result);
TableLayout tv = (TableLayout) findViewById(R.id.table);
tv.removeAllViewsInLayout();
int flag = 1;
for (int i = -1; i < jArray.length() - 1; i++) {
TableRow tr = new TableRow(Cohorts.this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
if (flag == 1) {
TextView b6 = new TextView(Cohorts.this);
b6.setText("Cohort Id");
b6.setTextColor(Color.BLUE);
b6.setTextSize(15);
tr.addView(b6);
TextView b19 = new TextView(Cohorts.this);
b19.setPadding(10, 0, 0, 0);
b19.setTextSize(15);
b19.setText("Cohort Name");
b19.setTextColor(Color.BLUE);
tr.addView(b19);
TextView b29 = new TextView(Cohorts.this);
b29.setPadding(10, 0, 0, 0);
b29.setText("Cohort Leader");
b29.setTextColor(Color.BLUE);
b29.setTextSize(15);
tr.addView(b29);
TextView b30 = new TextView(Cohorts.this);
b30.setText("Certified Leader");
b30.setTextColor(Color.BLUE);
b30.setTextSize(15);
tr.addView(b30);
TextView b35 = new TextView(Cohorts.this);
b35.setText("Associate Faculty");
b35.setTextColor(Color.BLUE);
b35.setTextSize(15);
tr.addView(b35);
TextView b39 = new TextView(Cohorts.this);
b39.setText("Cohort Location");
b39.setTextColor(Color.BLUE);
b39.setTextSize(15);
tr.addView(b39);
tv.addView(tr);
final View vline = new View(Cohorts.this);
vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 2));
vline.setBackgroundColor(Color.BLUE);
tv.addView(vline);
flag = 0;
} else {
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag", "Cohort ID: " + json_data.getInt("Cohort_ID") + ", Cohort Name: " + json_data.getString("Cohort_Name") + ", Cohort Leader" + json_data.getString("Cohort_Leader") + ", Certified Leader: " + json_data.getString("Certified_Leader") + ", Associate Faculty" + json_data.getString("Associate_Faculty") + ", Cohort Location" + json_data.getString("Cohort_Location"));
TextView b = new TextView(Cohorts.this);
String stime = String.valueOf(json_data.getInt("Cohort_ID"));
b.setText(stime);
b.setTextColor(Color.RED);
b.setTextSize(15);
tr.addView(b);
TextView b1 = new TextView(Cohorts.this);
b1.setPadding(10, 0, 0, 0);
b1.setTextSize(15);
String stime1 = json_data.getString("Cohort_Name");
b1.setText(stime1);
b1.setTextColor(Color.BLACK);
tr.addView(b1);
TextView b2 = new TextView(Cohorts.this);
b2.setPadding(10, 0, 0, 0);
String stime2 = json_data.getString("Cohort_Leader");
b2.setText(stime2);
b2.setTextColor(Color.BLACK);
b2.setTextSize(15);
tr.addView(b2);
TextView b3 = new TextView(Cohorts.this);
String stime3 = String.valueOf(json_data.getInt("Certified_Leader"));
b3.setText(stime3);
b3.setTextColor(Color.BLACK);
b3.setTextSize(15);
tr.addView(b3);
TextView b4 = new TextView(Cohorts.this);
b4.setPadding(10, 0, 0, 0);
b4.setTextSize(15);
String stime4 = json_data.getString("Associate_Faculty");
b4.setText(stime4);
b4.setTextColor(Color.BLACK);
tr.addView(b4);
TextView b5 = new TextView(Cohorts.this);
b5.setPadding(10, 0, 0, 0);
String stime5 = json_data.getString("Cohort_Location");
b5.setText(stime5);
b5.setTextColor(Color.BLACK);
b5.setTextSize(15);
tr.addView(b5);
tv.addView(tr);
final View vline1 = new View(Cohorts.this);
vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
vline1.setBackgroundColor(Color.WHITE);
tv.addView(vline1);
}
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data" + e.toString());
Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
}
}
}