从Android的WCF消耗JSON数组

时间:2016-08-27 17:31:33

标签: android arrays json wcf gson

所以,我坐着有点问题!我试图从我的WCF中使用其中一种方法。我有很多不同的方法来自WCF我没有遇到任何问题,但是,我特别在努力解决这个问题。

现在,我从我的服务中获得的回复是;

{
"GetUserProfileResult":
    {
"City":"Viborg",
"Country":"Danmark",
"FirstName":"Jens",
"Floor":"1",
"HouseNumber":"Vejnavnet",
"LastName":"Hansen",
"MiddleName":"Hans",
"Phone":"12345678",
"StreetName":"Vejnavnet",
"ZipCode":"8800"
    }
}

然后我有一个像这样设置的包装类;

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 + ".";
    }

}

现在,我猜测通过我的AsyncTask类的方式,我试图消费它的问题所在。但是,我已经尝试了10种不同的方法来做到这一点,它仍然无法正常工作。目前我正在尝试这个;

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<List<String>>() {}.getType();
            List<String> 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;
        }
    }

但那只是给了我一个;

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

我试过这样做;

Type listType = new TypeToken<List<Post>>(){}.getType();
List<Post> posts = (List<UserWrapper>) gson.fromJson(in, listType);

但这引发了同样的异常。任何帮助将不胜感激,因为我开始在这里拔出我的头发! :P

编辑1:

尝试直接将其解析为对象;

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;

并添加;

public String GetUserProfileResult;

在包装器类中,也不起作用。

编辑2:

事实证明,我并没有尝试使用数组,而是使用对象。这就是引起头痛的原因。我没有提到,我可以访问webservice,所以我只是重写了WCF服务,因此它返回List<string>而不是我的User类的实例。然后我开始工作了。感谢那个试图帮助的人,我 - 当他的答案没有成功时,删除了他的答案并低估了我的问题。

0 个答案:

没有答案