XML : Handling data passed to a result argument of AsyncTask.onPostExecute()

I have this private class that is within my main activity, and I am using it pull a JSon object off of my server into my app. The code below works fine and will display the JSon object as a string.

     private class HttpAsyncTask extends AsyncTask<String, Void, String> {          @Override          protected String doInBackground(String... urls) {                return httpBuild(urls[0]);          }          // onPostExecute displays the results of the AsyncTask.          @Override          protected void onPostExecute(String result) {              Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show();              etResponse.setText(result);          }      }    

what I am trying to do is place change the onPostExecute() method so it acts like webResult = result where webResult is an instance variable of the class mainActivity The problem is once I do this when I try to put the below code into the onCreate() method after HTTpAsyncTask has been called the app fails to display the object and crashes.

      public class MainActivity extends Activity {        private static final String mainSite = "http://mysitehere";        private String webResult;  //    private JSONArray floorsInBuilding, roomsInGender;  //    private JSONObject room;  //    private JSONArray arrayOfFloors;  //    private JSONObject room, arrayOfRooms;        EditText etResponse;      TextView tvIsConnected;      @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);            // get reference to the views          etResponse = (EditText) findViewById(R.id.etResponse);          tvIsConnected = (TextView) findViewById(R.id.tvIsConnected);            // check if you are connected or not          if(isConnected()){              tvIsConnected.setBackgroundColor(0xFF00CC00);              tvIsConnected.setText("You are conncted");          }          else{              tvIsConnected.setText("You are NOT conncted");          }            // call AsynTask to perform network operation on separate thread          new HttpAsyncTask().execute(this.buildBuildingAddress(8));          Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show();          etResponse.setText(WebResult);      }    

I'm wondering what makes the part of the code that displays the result dependent on the HttpAsyncTask. I'm also wondering how I can get the result of the HttpAsyncTask and store it as a string in the main class.

A good chunk of my code is based of of this example. http://hmkcode.com/android-parsing-json-data/

I'm sorry If my knowledge of android isn't so great but my experience lies in more in java.

No comments:

Post a Comment