1

次のように、NameValuePair の値として Hashtable を追加します。

name= credit_card

value(this is the hashtable)= {expirationDate="2013/02/18", ownerName="Jack Sparrow", typeOfCard="C2"}

またはこのように:

new BasicNameValuePair("credit_card",{expirationDate="2013/02/18", ownerName="Jack Sparrow", typeOfCard="C2"})。

これは私のコードの一部であり、単純な NameValuePair を追加する方法を確認できます。

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("https://example.com/register");
try {
  List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  nameValuePairs.add(new BasicNameValuePair("user", user));
  nameValuePairs.add(new BasicNameValuePair("password", password));
  post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

  HttpResponse response = client.execute(post);

前もって感謝します!

4

2 に答える 2

2

Apache DigestUtilsを使用して、ハッシュを非常に簡単に計算できます。

String hash = DigestUtils.sha512Hex(json);
nameValuePairs.add(new BasicNameValuePair("credit_card", hash));

または、Java 暗号化 API を直接使用することもできます。

final Charset charset = Charset.forName("UTF-8");
final MessageDigest digest = MessageDigest
        .getInstance("SHA-512");
final byte[] hashData = digest
        .digest(json.getBytes(charset));
final String hash = new String(hashData, charset);
nameValuePairs.add(new BasicNameValuePair("credit_card", hash));
于 2013-02-19T00:48:08.317 に答える
2

あなたの質問を正しく理解しているかどうかわかりません。しかし、私はここで試しています-

String message = "hello";

System.out.println( DigestUtils.md5Hex(message) );

したがって、以下のようなadd方法を変更できます。nameValuePairs

new BasicNameValuePair("credit_card", DigestUtils.md5Hex(your_json))

マニュアルはこちら

質問が更新された後に更新された回答

HashTable の値が文字列の場合は、次のように追加できますnameValuePair

new BasicNameValuePair("credit_card", (your_json))

于 2013-02-19T01:03:50.027 に答える