3

我对我的 .NET Web Api 的 Android 应用程序有 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"
}

我应该怎么做才能实现这一目标?

提前致谢。

4

2 回答 2

3

您的第一个问题在这里:

 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"}

如果您可以发布错误消息,我将尝试帮助调试。

于 2013-05-11T01:21:56.397 回答
0

如果您使用 gson 对对象进行序列化,则需要使用 URLEncoder.encode(yourClass, "UTF-8");

  Gson gson = new Gson();
  String objectSerializable;

  objectSerializable= gson.toJson(yourObject, yourClass.class).toString();

  pedidoSerializado = URLEncoder.encode(objectSerializable, "UTF-8");
  String urlPathService = "http://xxx.xxx.xxx/RestfulAPI/api?object="+objectSerializable;

对不起我的英语不好。

于 2015-11-22T01:02:07.987 回答