2014-12-03 26 views
0

我是JSON新手,正在研究Coinbase API。 See here如何在Java中通过HTTP POST JSON数据?

所以我试图发布此数据通过HTTP:

{ 
    "transaction": { 
    "to": "[email protected]", 
    "amount": "1.234", 
    "notes": "Sample transaction for you" 
    } 
} 

我将如何做到这一点在Java中?

我知道如何创建一个程序来GET一个响应但不POST如何将要求

这里是我的响应计划:

import java.io.IOException; 
import java.security.InvalidKeyException; 
import java.security.NoSuchAlgorithmException; 

import javax.crypto.Mac; 
import javax.crypto.spec.SecretKeySpec; 

import org.apache.commons.codec.binary.Hex; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.client.methods.HttpRequestBase; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.HttpClientBuilder; 
import org.apache.http.util.EntityUtils; 

import com.coinbase.api.Coinbase; 
import com.coinbase.api.CoinbaseBuilder; 

public class CoinbaseExample { 

    static String API_KEY = "My API KEY"; 

    static String API_SECRET = "MY API SECRET"; 

    public static String getHttp(String url, String body) 
      throws InvalidKeyException, NoSuchAlgorithmException, 
      ClientProtocolException, IOException { 

     String nonce = String.valueOf(System.currentTimeMillis()); 
     String message = nonce + url + (body != null ? body : ""); 

     Mac mac = Mac.getInstance("HmacSHA256"); 
     mac.init(new SecretKeySpec(API_SECRET.getBytes(), "HmacSHA256")); 
     String signature = new String(Hex.encodeHex(mac.doFinal(message.getBytes()))); 

     HttpRequestBase request; 
     if (body == null || body.length() == 0) 
      request = new HttpGet(url); 
     else { 
      HttpPost post = new HttpPost(url); 
      post.setEntity(new StringEntity(body)); 
      request = post; 
     } 
     request.setHeader("ACCESS_KEY", API_KEY); 
     request.setHeader("ACCESS_SIGNATURE", signature); 
     request.setHeader("ACCESS_NONCE", nonce); 

     HttpClient httpClient = HttpClientBuilder.create().build(); 
     HttpResponse response = httpClient.execute(request); 

     HttpEntity entity = response.getEntity(); 
     if (entity != null) 
      return EntityUtils.toString(entity); 
     return null; 
    } 

} 

如何创建此类似程序要求?

+0

你尝试过自己? – 2014-12-03 08:56:15

+0

亚..我试过自己..但总是卡住! :(这就是为什么我来这里问... :(请帮助! – 2014-12-03 08:57:55

+0

解释这个问题,你得到什么?一个错误?请求没有发送,它是空的?... – Yazan 2014-12-03 09:04:52

回答

0

试试这个

String url = "http://yoururl.com"; 
URL obj = new URL(url); 
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); 

//add reuqest header 
con.setRequestMethod("POST"); 
con.setRequestProperty("User-Agent", USER_AGENT); 
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); 

String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345"; 

// Send post request 
con.setDoOutput(true); 
DataOutputStream wr = new DataOutputStream(con.getOutputStream()); 
wr.writeBytes(urlParameters); 
wr.flush(); 
wr.close(); 

int responseCode = con.getResponseCode(); 
System.out.println("\nSending 'POST' request to URL : " + url); 
System.out.println("Post parameters : " + urlParameters); 
System.out.println("Response Code : " + responseCode); 

BufferedReader in = new BufferedReader(
     new InputStreamReader(con.getInputStream())); 
String inputLine; 
StringBuffer response = new StringBuffer(); 

while ((inputLine = in.readLine()) != null) { 
    response.append(inputLine); 
} 
in.close(); 

//print result 
System.out.println(response.toString()); 
+0

什么是在你的代码中的'urlParameters'? – 2014-12-03 08:59:31

+0

参数是一个名字值对,在里你会发送你的json – 2014-12-03 09:00:12

+0

什么是你的程序中的USER_AGENT'它给我一个错误..这个没有定义.. – 2014-12-03 09:01:37

相关问题