First and foremost, I am an android newbie.
I am creating a simple app which pulls some data from the server through a PHP script and showing them in a ListView in the next Activity. However, I find that if the script returns nothing, the app crashes. So I kept a check that only if the script returns some data, the app would switch to the next activity.
This is my code.
public class HomeScreen extends ActionBarActivity {
String message3;
String message_short;
String[] items;
String[] short_items;
int check = 0;
private ProgressDialog dialog;
public String readJSONFeed(String URL)
{
StringBuilder sb = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet hg = new HttpGet(URL);
try
{
HttpResponse response = client.execute(hg);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if(statusCode == 200)
{
HttpEntity en = response.getEntity();
InputStream content = en.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while((line = reader.readLine()) != null)
{
sb.append(line);
}
}
else
{
Log.e("JSON", "Failed to download File");
}
}
catch(ClientProtocolException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
return sb.toString();
}
private class ReadJSONFeedTask extends AsyncTask<String, Void, String>
{
protected void onPreExecute()
{
super.onPreExecute();
dialog = new ProgressDialog(HomeScreen.this);
dialog.setMessage("Downloading Notifications. Please wait . . .");
dialog.setIndeterminate(false);
dialog.setCancelable(false);
dialog.show();
}
protected String doInBackground(String...urls)
{
return readJSONFeed(urls[0]);
}
protected void onPostExecute(String result)
{
dialog.dismiss();
try
{
JSONArray jsonArray = new JSONArray(result);
items = new String[jsonArray.length()];
short_items = new String[jsonArray.length()];
for(int i = 0; i < jsonArray.length(); i++)
{
JSONObject jobj = jsonArray.getJSONObject(i);
message3 = "SUBJECT : " + jobj.getString("subject") + "\n\n" +
"DATE : " + jobj.getString("date") + "\n" + jobj.getString("time") + "\n\n"
+ "NOTICE : " + jobj.getString("notice");
message_short = "SUBJECT : " + jobj.getString("subject") + "\n"
+ "NOTICE : " + jobj.getString("notice").substring(0, 20) + "..."
+ "\n" + jobj.getString("time");
items[i] = message3;
short_items[i] = message_short;
check += 1;
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
}
public void notificationPane(View view)
{
String localhost = "http://10.0.2.2/example/json/notification.php";
new ReadJSONFeedTask().execute(localhost);
if(check != 0)
{
Intent i = new Intent(HomeScreen.this, NotificationPanel.class);
i.putExtra("items", items);
i.putExtra("short_items", short_items);
startActivity(i);
}
else
{
Toast.makeText(getBaseContext(), "Either there is no notification to display or there might be a problem with your internet connection.", Toast.LENGTH_SHORT).show();
}
}
public void recentNotice(View view)
{
String localhost = "http://10.0.2.2/example/json/recent_notification.php";
new ReadJSONFeedTask().execute(localhost);
if(check != 0)
{
Intent i = new Intent(HomeScreen.this, NotificationPanel.class);
i.putExtra("items", items);
i.putExtra("short_items", short_items);
startActivity(i);
}
else
{
Toast.makeText(getBaseContext(), "Either there is no notification to display or there might be a problem with your internet connection.", Toast.LENGTH_SHORT).show();
}
}
}
The int check
is where I check whether the server has returned any JSON data. If the check
is not 0, it does not switch to the next activity
.
But problem with this code is that when I click the button, it shows me the TOAST Either there is no notification to display or there might be a problem with your internet connection.
even before it has pulled data from the server, thus it never goes to the next Activity NotificationPanel.class
. I understand this is due to the AsyncTask
. What is more weird is if I keep clicking the button several times, the app suddenly switches to the next activity showing the data in ListView
. But it doesn't always work. Is there a work around to this problem?
Please help.
Thank you in advance.
Move your code with if else to the last in onPostExecute. AsyncTask is still in execution when you check the result.