I've got an app that makes API calls when the user logs in, I've got two classes, one that Pushes to the API and one that Pulls from it. I've also got an asyncTask
on my LogIn Activity which handles the network connection. When the network connection starts I have a view switcher
to switch the view to a progress loader and a textview
, the textview is used to display stuff like "Connecting", "Downloading Data", ect.
The problem is my API Push and Pull methods are stored in different classes and the LogIn AsyncTask
simply calls them, it all works except for updating the TextView
to let the user know the progress.
In the LogIn activity, I have this method, which changes the textview
info to whichever number is passed to it.
public void updateProgress(int i) {
switch (i) {
case 0:
loadInfo.setText(R.string.log_in_thread_Connecting);
break;
case 1:
loadInfo.setText(R.string.log_in_thread_Connected);
break;
case 2:
loadInfo.setText(R.string.log_in_Thread_Sending_Data);
break;
case 3:
loadInfo.setText(R.string.log_in_thread_Response);
break;
case 4:
loadInfo.setText(R.string.log_in_web_connecting);
break;
case 5:
loadInfo.setText(R.string.log_in_web_connected);
break;
case 6:
loadInfo.setText(R.string.log_in_web_user_data_download);
break;
case 7:
loadInfo.setText(R.string.log_in_web_user_data_downloaded);
break;
case 8:
loadInfo.setText(R.string.log_in_web_device_data_upload);
break;
}
}
This works if i'm calling it from the AsyncTask
from the LogIn Activity but not from the API classes.
I have found a method called .runOnUiThread so I tried to implement that.
private void publishProgress(final int i){
logInActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
logInActivity.updateProgress(i);
Log.d("UI THREAD","HIT");
}
});
}
This is what I have come up with, but it doesn't seem to be hitting, I don't even see the Log post.
I did also have an error implementing this but it was resolved when I added Looper.prepare();
to the Log in AsyncTask
AsyncTask
private class RegistrationWeb extends AsyncTask<Void,Void,Void>{
private static final String tag = "API_LogIn";
private String APIKey = null
@Override
protected void onPreExecute() {
super.onPreExecute();
viewFlipper.showNext();
APIKey = getAPIKey();
}
@Override
protected Void doInBackground(Void... voids) {
try{
Looper.prepare();
GetAPIData get = new GetAPIData();
URL url = new URL(API_Register_User+APIKey);
String response = get.GetData(url);
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObject = jsonArray.getJSONObject(0);
//If the user is accepted
if(jsonObject.getString("_Code").equals("0")){
PublishProgress(8);
PostAPIData post = new PostAPIData();
url = new URL(API_Register_Device);
response = post.PostData(url);
jsonArray = new JSONArray(response);
jsonObject = jsonArray.getJSONObject(0);
} else if(jsonObject.getString("_Code").equals("2")){
}
} catch (MalformedURLException e) {
Log.e(tag,e.toString());
} catch (JSONException e) {
Log.e(tag,e.toString());
} catch (IOException e) {
Log.e(tag,e.toString());
}
return null;
}
private void PublishProgress(final int i){
runOnUiThread(new Runnable() {
@Override
public void run() {
updateProgress(i);
}
});
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}
}