title: Assign text read from file in network to TextView in Android fragment
body: <p>I am trying to read a file over the network and assign the returned text to a TextView widget of a Fragment. The file is read in an Async thread, after being called by a method in ManActivity. The codes are given below:</p>
  
  <pre><code>MainActivity.java (relevant part):
  
  public void myClickHandler(View view){
      ConnectivityManager connmngr=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 
      NetworkInfo networkinfo=connmngr.getActiveNetworkInfo();
      MainActivity ma=new MainActivity();
      if(networkinfo!=null &amp;&amp; networkinfo.isConnected()){
          new Fragment_2().new DownloadWebpageTask(ma).execute(stringurl);
      }else{
          textview.setText("No network connection available");
      }
  }
  }
  
  //Called by the OnPostExecute() method
  public void displayText(String result){
      DateFormat df=DateFormat.getDateInstance(DateFormat.LONG);
      java.util.Date d=new java.util.Date();
      String display=result + " " + df.format(d);
      Log.d("DOWNLOAD",display);
      textview=(TextView)findViewById(R.id.web_text);
      textview.setText(display);
  }
  
  
  Fragment_2.java
  public class Fragment_2 extends Fragment {
  
  private static final String DEBUG_TAG="HttpExample";
  MainActivity ma;
  String result;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
      View rootView = inflater.inflate(R.layout.fragment_2, container, false);
      TextView textview=(TextView)rootView.findViewById(R.id.web_text);
      textview.setText(result);
      return rootView;
  }
  
  //Call AsyncTask to create a new, background network thread which is separate from the UI thread
  public class DownloadWebpageTask extends AsyncTask&lt;String,String,String&gt;{
  
      MainActivity ma;
  
      DownloadWebpageTask(MainActivity ma){
          this.ma=ma;
      }
  
      @Override
      protected String doInBackground(String... urls) {
          try{
              result=downloadUrl(urls[0]);
          }catch(Exception e){
              e.printStackTrace();
              result="Exception while reading the text";
           }
           return result;
       }
  
      @Override
      protected void onPostExecute(String result){
          ma.displayText(result);
      }
      //downloadUrl fetches and processes the web page content. After  processing, it returns a result string.
      private String downloadUrl(String myurl) throws IOException{
          InputStream is=null;
          String str=null;
  
          try{
              URL url=new URL(myurl);
              Log.d("URL:",url.toString());
              HttpURLConnection conn=(HttpURLConnection)  url.openConnection();
              conn.setReadTimeout(20000);
              conn.setConnectTimeout(30000);
              conn.setRequestMethod("GET");
              conn.setDoInput(true);
              Log.d("TRY DOWNLOAD","Inside try block of downloadUrl");
              //Start the query
              conn.connect();
              int response=conn.getResponseCode();
              Log.d(DEBUG_TAG,"The response is : " + response);
              BufferedReader in=new BufferedReader(new  InputStreamReader(url.openStream()));
              StringBuilder sb=new StringBuilder();
              while((str=in.readLine())!=null){
                  sb.append(str);
              }
              return sb.toString();
          }finally{
              //input stream should be closed after the app has finished using it
              if(is!=null){
                  is.close();
              }
          }   
      }
  }
  }
  </code></pre>
  
  <p>onPostExecute() calls the displayText() method in the MainActivity. However, i need the text to appear in a textview of the fragment. So I need to change the code in the onCreateView() method of Fragment_2.java, I guess. </p>
  
  <p>But I don't know how to do so exactly. Please help. </p>