title: Get data from a web server in Objective C
body: <p>I have a very convenient way of getting data from a web server in my Android app, and would like to be able to duplicate that method for iOS. </p>
  
  <p>In Android I have a Async Class that I call from other classes and it returns data from server when it has finished loading.</p>
  
  <p>This is how that class looks like:</p>
  
  <p>public class GetData extends AsyncTask, String, Void> {</p>
  
  <pre><code>public Context context;
  InputStream is = null;
  StringBuilder sb = null;
  
  public GetData (Context context) {
      this.context = context;
  }
  
  @Override
  /**
   * Send the values in params to the server
   */
  protected Void doInBackground(ArrayList&lt;NameValuePair&gt;... params) {
  
      String server = "mywebpage";
      HttpPost httppost=new HttpPost(server);
      HttpClient httpclient=new DefaultHttpClient();
  
      try {
          httppost.setEntity(new UrlEncodedFormEntity(params[0]));
          HttpResponse rs=httpclient.execute(httppost);
          HttpEntity entity = rs.getEntity();
          is = entity.getContent();
  
          try{
              BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8);
  
              sb = new StringBuilder();
              sb.append(reader.readLine());
              String line = "0";
              while ((line = reader.readLine()) != null) {
                  sb.append(line + "\n");
              }
          }
          catch(Exception e) {
              Log.e("log_tag", "Error converting result "+e.toString());
          }
          is.close();
          publishProgress(sb.toString());
  
      }catch (UnsupportedEncodingException e) {
          e.printStackTrace();
      } catch (ClientProtocolException e) {
          e.printStackTrace();
      }catch (IOException e) {
          e.printStackTrace();
      }
  
  
      return null;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>If I want to use the GetData class I create a new instance of it like so:</p>
  
  <p>new GetData(this.getActivity()) {</p>
  
  <pre><code>        @Override
          protected void onProgressUpdate(String... string) {
              result = string[0]);
  
          }
      }.execute(data);
  </code></pre>
  
  <p>I have been able to get and send data from within each viewController in iOS but I haven't been able to get a similar method to this one to work.</p>
  
  <p>I have been trying to create a Class but I'm not sure how to call it with certain parameters and what subclass I should make GetData be of.</p>
  
  <p>Is there any good way to do this in Objective C?</p>