title: How to call a RESTful API (ASP.NET) from Android App?
body: <p>I followed the tutorial here: <a href="http://www.tutecentral.com/restful-api-for-android-part-1/" rel="nofollow">http://www.tutecentral.com/restful-api-for-android-part-1/</a> to create a RESTful API using ASP.NET and all is well. At the end of the tutorial I downloaded a ZIP file which contains a Java file with calls to the methods from the API.</p>
  
  <p>Question is...how do I call these methods so they interact with my web service API? I'm really confused, I have done similar things in the past using PHP on my web server and inserting/extracting SQL data but never with ASP.NET or a setup similar to this.</p>
  
  <p>If it helps, here are the contents of the Java file I get at the end of that tutorial:</p>
  
  <pre><code>public class RestAPI {
  private final String urlString = "http://localhost:53749/Gen_Handler.ashx";
  
  private static String convertStreamToUTF8String(InputStream stream) throws IOException {
      String result = "";
      StringBuilder sb = new StringBuilder();
      try {
          InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
          char[] buffer = new char[4096];
          int readChars = 0;
          while (readChars != -1) {
              readChars = reader.read(buffer);
              if (readChars &gt; 0)
                 sb.append(buffer, 0, readChars);
          }
          result = sb.toString();
      } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
      }
      return result;
  }
  
  private String load(String contents) throws IOException {
      URL url = new URL(urlString);
      HttpURLConnection conn = (HttpURLConnection)url.openConnection();
      conn.setRequestMethod("POST");
      conn.setConnectTimeout(60000);
      conn.setDoOutput(true);
      conn.setDoInput(true);
      OutputStreamWriter w = new OutputStreamWriter(conn.getOutputStream());
      w.write(contents);
      w.flush();
      InputStream istream = conn.getInputStream();
      String result = convertStreamToUTF8String(istream);
      return result;
  }
  
  private Object mapObject(Object o) {
      Object finalValue = null;
      if (o.getClass() == String.class) {
          finalValue = o;
      }
      else if (Number.class.isInstance(o)) {
          finalValue = String.valueOf(o);
      } else if (Date.class.isInstance(o)) {
          SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss", new Locale("en", "USA"));
          finalValue = sdf.format((Date)o);
      }
      else if (Collection.class.isInstance(o)) {
          Collection&lt;?&gt; col = (Collection&lt;?&gt;) o;
          JSONArray jarray = new JSONArray();
          for (Object item : col) {
              jarray.put(mapObject(item));
          }
          finalValue = jarray;
      } else {
          Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();
          Method[] methods = o.getClass().getMethods();
          for (Method method : methods) {
              if (method.getDeclaringClass() == o.getClass()
                      &amp;&amp; method.getModifiers() == Modifier.PUBLIC
                      &amp;&amp; method.getName().startsWith("get")) {
                  String key = method.getName().substring(3);
                  try {
                      Object obj = method.invoke(o, null);
                      Object value = mapObject(obj);
                      map.put(key, value);
                      finalValue = new JSONObject(map);
                  } catch (Exception e) {
                      e.printStackTrace();
                  }
              }
          }
      }
      return finalValue;
  }
  
  public JSONObject CreateNewAccount(String firstName,String lastName,String userName,String password) throws Exception {
      JSONObject result = null;
      JSONObject o = new JSONObject();
      JSONObject p = new JSONObject();
      o.put("interface","RestAPI");
      o.put("method", "CreateNewAccount");
      p.put("firstName",mapObject(firstName));
      p.put("lastName",mapObject(lastName));
      p.put("userName",mapObject(userName));
      p.put("password",mapObject(password));
      o.put("parameters", p);
      String s = o.toString();
      String r = load(s);
      result = new JSONObject(r);
      return result;
  }
  
  public JSONObject GetUserDetails(String userName) throws Exception {
      JSONObject result = null;
      JSONObject o = new JSONObject();
      JSONObject p = new JSONObject();
      o.put("interface","RestAPI");
      o.put("method", "GetUserDetails");
      p.put("userName",mapObject(userName));
      o.put("parameters", p);
      String s = o.toString();
      String r = load(s);
      result = new JSONObject(r);
      return result;
  }
  
  public JSONObject UserAuthentication(String userName,String passsword) throws Exception {
      JSONObject result = null;
      JSONObject o = new JSONObject();
      JSONObject p = new JSONObject();
      o.put("interface","RestAPI");
      o.put("method", "UserAuthentication");
      p.put("userName",mapObject(userName));
      p.put("passsword",mapObject(passsword));
      o.put("parameters", p);
      String s = o.toString();
      String r = load(s);
      result = new JSONObject(r);
      return result;
  }
  
  public JSONObject GetDepartmentDetails() throws Exception {
      JSONObject result = null;
      JSONObject o = new JSONObject();
      JSONObject p = new JSONObject();
      o.put("interface","RestAPI");
      o.put("method", "GetDepartmentDetails");
      o.put("parameters", p);
      String s = o.toString();
      String r = load(s);
      result = new JSONObject(r);
      return result;
  }
  }
  </code></pre>
  
  <p>In my activity/fragment I tried doing this, but nothing is returned :(.</p>
  
  <pre><code>public void displaySafeMessage(View view) {
          RestAPI test = new RestAPI();
          JSONObject jsonObj = new JSONObject();
  
          try {
              //jsonObj = test.CreateNewAccount("tudor","hofnar","tudor.hofnar","password");
              jsonObj = test.GetDepartmentDetails();
          } catch (Exception e) {
          }
  
          String tmp = jsonObj.toString();
          Toast.makeText(getActivity(), tmp, Toast.LENGTH_LONG).show();
      }
  </code></pre>
  
  <p>Note: the displaySafeMessage() method is called from a button onClick() which I did not include.There are no errors in Android with this code but also nothing is returned, and I have values in the Departments table, as a SQL query shows. </p>
  
  <p>I know I'm missing a major thing here, so please let me know what it is!
  Thanks!</p>