The code in doInBackgroung
works perfectly in java but on android it just won't work. The output is nothing not even the table. Using Android Studio. The complete code is here for the android app.
Now the app fires up an AsyncTask
to connect to a server, the server just (PHP) echoes a few strings when done in java separately the strings are caught perfectly by the HttpURLConnection
. However, in Android the same code (the one in doInbackbround()
method of DownloadFilesTask
) won't reproduce the same effect.
I have tried to catch any exceptions that might occur but there is no exception being thrown. The code is pretty self explanatory for anyone who actually knows Android development.
About the debugging, well the function showdata()
does get called but the parameter string seems to be empty. I have added more comments in code to describe the problem a bit better.
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MyActivity";
void showData(String s){ //gets called with an empty string
TextView t = (TextView) findViewById(R.id.textView);
t.setText(s);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
setContentView(R.layout.activity_main);
// initAttendanceTable();
DownloadFilesTask task = new DownloadFilesTask();
task.execute();
initAttendanceTable();//just generates a tableRow and add it to a Table Row
// nothing fancy here but it actually is'nt doing anything after adding aynstask code
}
catch(Exception e) {
TextView t = (TextView) findViewById(R.id.textView);
t.setText(e.getLocalizedMessage()+"error");
}
}
private class DownloadFilesTask extends AsyncTask<Void,Void, String> {
protected String doInBackground(Void ...p) {
String results = new String("");
String temp = new String("");
try {
URL homeUrl = new URL("http://10.0.2.2:80/attendance/attendance_android.html");
//returns a few names ending with a null at the end
HttpURLConnection httpConn = (HttpURLConnection) homeUrl.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
if(br.ready()) {
results = br.readLine();
while(results!=null) {
results=br.readLine();
if(results!=null){
temp= temp + results;
}
}
}
}
catch(Exception e){
return null;
}
return temp;
}
//public ProgressDialog mProgressDialog;
protected void onProgressUpdate(Void ...progress) {
super.onProgressUpdate(progress);
//ProgressDialog.show(MainActivity.this, "loading", "wait...", true, true);
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result==null){ // to ctach error from doInbackground
Toast.makeText(getApplicationContext(), (String)result,
Toast.LENGTH_LONG).show();
}
showData(result);
}
}
private void initAttendanceTable() {
try{
TextView t=(TextView) findViewById(R.id.textView);
TableLayout attendanceTable = (TableLayout) findViewById(R.id.attendanceTable);
for (int j = 0; j < 3; j++) {
TableRow tbRow = new TableRow(this);
tbRow.setId(j);
tbRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
tbRow.setGravity(View.FOCUS_LEFT);
tbRow.setPadding(1, 1, 1, 1);
for (int i = 0; i < 3; i++) {
TextView tbRowText = new TextView(this);
tbRowText.setLayoutParams(new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.33f));
// tbRowText.setText(br.readLine());
tbRow.addView(tbRowText);
}
attendanceTable.addView(tbRow);
}
}
catch(Exception e){
TextView t=(TextView) findViewById(R.id.textView);
t.setText(e.getLocalizedMessage());
}
}
@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_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}