用volley管理请求出错? 400 报错
@stormzhang 你好,想跟你请教个问题:我用你在“9gag”项目中的方式管理了请求,在GosnRequst中
protected Response<T> parseNetworkResponse(NetworkResponse response) { try { String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers)); return Response.success(mGson.fromJson(json, mClazz),HttpHeaderParser.parseCacheHeaders(response)); } catch (UnsupportedEncodingException e) { return Response.error(new ParseError(e)); } catch (JsonSyntaxException e) { return Response.error(new ParseError(e)); }
其中
Sting json="[{"ID":"5","name":"test","psd":"123","create_date":"2014-04-19 13:59:47"}]";
但是,每次运行到
mGson.fromJson(json, mClazz)
的时候 就会抛
JsonSyntaxException
异常
我的mClass是
User.UserRequestData.class
public class User extends BaseModel{ private static final HashMap<String, User> CACHE = new HashMap<String, User>(); public String ID; public String name; public String psd; public String create_date; private static void addToCache(User user) { CACHE.put(user.ID, user); } private static User getFromCache(String id) { return CACHE.get(id); } public static User fromJson(String json) { return new Gson().fromJson(json, User.class); } public static User fromCursor(Cursor cursor) { String id = cursor.getString(cursor.getColumnIndex(UserDataHelper.UsersDBInfo.ID)); User user = getFromCache(id); if (user != null) { return user; } user = new Gson().fromJson( cursor.getString(cursor.getColumnIndex(UserDataHelper.UsersDBInfo.NAME)), User.class); addToCache(user); return user; } public static class UserRequestData { public ArrayList<User> data; } }
请问到达时哪里出问题了
万分感谢!!
问题已解决:是因为
public static class UserRequestData { public ArrayList<User> data; }
与
Sting json="[{"ID":"5","name":"test","psd":"123","create_date":"2014-04-19 13:59:47"}]";
json数据应该是
{"data":[{"ID":"5","name":"test","psd":"123","create_date":"2014-04-19 13:59:47"}]}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。