3

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

これを達成するにはどうすればよいですか?

前もって感謝します。

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"},"ユーザー名":"ME"}

エラー メッセージを投稿していただければ、デバッグをお手伝いします。

于 2013-05-11T01:21:56.397 に答える