Android GSON序列化问题
作者:互联网
我的Android应用程序上有对.NET Web Api的HttpPost请求.
我正在使用json数据类型和gson类来序列化我的对象.
这是我有关序列化的代码:
HttpPost post = new HttpPost();
post.setURI(new URI("my url goes here"));
HashMap<String, String> hashMap = new HashMap<String, String>();
hashMap.put("username", username);
hashMap.put("userCredentials", new Gson().toJson(credentials, UCredentials.class));
// username and credentials are parameters.
post.setHeader("Content-Type", "application/json; charset=utf-8");
post.setEntity(new StringEntity(new Gson().toJson(hashMap)));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(post);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
// If successful.
}
hashMap.put(“ userCredentials”,new Gson().toJson(credentials,UCredentials.class))除外;代码行一切正常.但是有了这条线…
Gson类为我提供了json作为:
{
"userCredentials":
"{
\"emailOrUsername\":\"asd@sadas.com\",
\"password\":\"113\"
}",
"username":"sdggfsdgf"
}
这不是有效的json模式.我需要像这样的json输出:
{
"userCredentials":
{
"emailOrUsername":"asd@sadas.com",
"password":"113"
},
"username":"sdggfsdgf"
}
我应该怎么做才能做到这一点?
提前致谢.
解决方法:
您的第一个问题在这里:
hashMap.put("userCredentials", new Gson().toJson(credentials, UCredentials.class));
HashMap中使用的是一个可JSON解析的字符串.当您跟进时:
post.setEntity(new StringEntity(new Gson().toJson(hashMap)));
该字符串被转义,因为它不是对象.
您真正想要做的是这样的:
HttpPost post = new HttpPost();
post.setURI(new URI("my url goes here"));
// Here we say that the Hashmap takes Object as its value.
HashMap<String, String> hashMap = new HashMap<String, Object>();
hashMap.put("username", username);
// Here we put the credentials object in the HashMap
hashMap.put("userCredentials", credentials);
// username and credentials are parameters.
post.setHeader("Content-Type", "application/json; charset=utf-8");
Type mapType = new TypeToken<HashMap<String, Object>>() {}.getType();
post.setEntity(new StringEntity(new Gson().toJson(hashMap, mapType)));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse httpResponse = httpClient.execute(post);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
// If successful.
}
这应该做您想要的,但是由于您说它不起作用….
我不确定UCredentials类.它似乎是您自己的代码.这是我的猜测:
public class UCredentials {
public String emailOrUsername;
public String password;
}
这是简化的代码:
import java.lang.reflect.Type;
import java.util.HashMap;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
public class Test {
public static void main(String[] args) {
Gson gson = new Gson();
HashMap<String, Object> hashMap = new HashMap<String, Object>();
UCredentials uc = new UCredentials();
uc.emailOrUsername = "me@somehost.com";
uc.password = "pAsSwOrD";
hashMap.put("username", "ME");
hashMap.put("userCredentials", uc);
Type mapType = new TypeToken<HashMap<String, Object>>() {}.getType();
System.out.println(gson.toJson(hashMap, mapType));
}
}
这是输出:
{“userCredentials”:{“emailOrUsername”:”me@somehost.com”,”password”:”pAsSwOrD”},”username”:”ME”}
如果您可以发布错误消息,我将尽力帮助调试.
标签:gson,http-post,android 来源: https://codeday.me/bug/20191123/2066677.html