我使用 asp.net web api 作为服务器,使用 android 作为客户端。我从 android 请求到 web api,我得到了 json 字符串。当我使用 Gson 将此 json 字符串分配给 java 对象时,我得到了异常。我的代码是,
private StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
// Read response until the end
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
Log.i("RETURN DATA:", total.toString());
Gson gson = new Gson();
ReturnData returndata = (ReturnData)
gson.fromJson( total.toString(), ReturnData.class);
Log.i("RESULT DATA", returndata.ResultData.toString());
} catch (IOException e) {
e.printStackTrace();
}
// Return full string
return total;
}
而我的 json 字符串(我从服务器获取)就像,
"\"OperationResult\":0,\"Messages\":operationsuccess\"\",\"UpdateAvailable\":\"\",\"ResultData\":{\"SessionId\":1584789522,\"UserName\":vinoth,\"AccoundId\":1236985,\"Roles\":[],\"DisplayName\":Vinoth,\"Status\":0,\"Type\":0}}"