title: Consuming JSON Array From WCF From Android
body: <p>So, I am sitting with a bit of a problem! I am trying to consume one of the methods from my WCF. I have plenty of different methods from the WCF that I am consuming with no problem, however, I am struggling with this one in particular.</p>
  
  <p>Now, the response I am getting from my service is;</p>
  
  <pre><code>{
  "GetUserProfileResult":
      {
  "City":"Viborg",
  "Country":"Danmark",
  "FirstName":"Jens",
  "Floor":"1",
  "HouseNumber":"Vejnavnet",
  "LastName":"Hansen",
  "MiddleName":"Hans",
  "Phone":"12345678",
  "StreetName":"Vejnavnet",
  "ZipCode":"8800"
      }
  }
  </code></pre>
  
  <p>I then have a wrapper class set up like so;</p>
  
  <pre><code>public class UserWrapper {
      public String City;
      public String Country;
      public String FirstName;
      public String Floor;
      public String HouseNumber;
      public String LastName;
      public String MiddleName;
      public String Phone;
      public String StreetName;
      public String ZipCode;
  
      @Override
      public String toString() {
          return "First Name: " + FirstName + ". Middle Name: " + MiddleName + ". Last Name: " + LastName + ". Streetname: " + StreetName + ". Housenumber: " + HouseNumber + ". City: " + City +
                  ". Zipcode: " + ZipCode + ". Country: " + Country + ". Phone: " + Phone + ".";
      }
  
  }
  </code></pre>
  
  <p>Now, I am guessing that the way, I am trying to consume it - through my AsyncTask class is where the problem is. However, I have tried 10 different ways to do this, and it still will not work. Currently I am trying this;</p>
  
  <pre><code>private boolean getUserProfile(String email) {
          try {
              gson = new GsonBuilder().create();
              URL url = new URL("http://website.servicelocation.com/Service1.svc/GetUserProfile/" + email);
              urlConnection = (HttpURLConnection)url.openConnection();
              urlConnection.setRequestMethod("GET");
              urlConnection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
              in = new BufferedReader(new InputStreamReader((urlConnection.getInputStream())));
              Type listType = new TypeToken&lt;List&lt;String&gt;&gt;() {}.getType();
              List&lt;String&gt; yourList = new Gson().fromJson(in, listType);
              if (yourList != null) {
                  for (String s : yourList) {
                      Log.d("List Contents", s);
                  }
                 return true;
              }
              Log.d("Error", "GetProfile Error");
              return false;
          } catch (MalformedURLException mue) {
              mue.printStackTrace();
              return false;
          } catch (IOException ioe) {
              ioe.printStackTrace();
              return false;
          } catch (Exception e) {
              e.printStackTrace();
              return false;
          }
      }
  </code></pre>
  
  <p>But that just gives me a;</p>
  
  <pre><code>com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $
  </code></pre>
  
  <p>I have tried doing it like so;</p>
  
  <pre><code>Type listType = new TypeToken&lt;List&lt;Post&gt;&gt;(){}.getType();
  List&lt;Post&gt; posts = (List&lt;UserWrapper&gt;) gson.fromJson(in, listType);
  </code></pre>
  
  <p>But this throws the same exception. Any help will be greatly appreciated, as I am starting to pull out my hairs here! :P</p>
  
  <p>EDIT 1:</p>
  
  <p>Trying to parse it directly as an object like so;</p>
  
  <pre><code>in = new BufferedReader(new InputStreamReader((urlConnection.getInputStream())));
              Log.d("in:", in.readLine());
              UserWrapper uw = gson.fromJson(in, UserWrapper.class);
              if (uw != null) {
                      Log.d("UserWrapper Content:", uw.toString());
                 return true;
              }
              Log.d("Error", "GetProfile Error");
              return false;
  </code></pre>
  
  <p>And adding;</p>
  
  <pre><code>public String GetUserProfileResult;
  </code></pre>
  
  <p>in the wrapper class, also does not work.</p>
  
  <p>EDIT 2:</p>
  
  <p>So as it turns out, I was not trying to consume an array, but an object. That was what was causing the headache. I failed to mention, that I have access to the webservice, so I just rewrote the WCF service, so that it returns a <code>List&lt;string&gt;</code> instead of an instance of my <code>User</code> class. Then I got it to work. Thanks to the guy who tried to help, me - and when his answer didn't work, deleted his answer and downvoted my question.</p>