應用程序向Bringg API發送HTTP請求。我想使用Android中的功能,所以我需要將php代碼轉換爲Java。如何將php-script轉換爲Java?
<?
$url = 'http://developer-api.bringg.com/partner_api/customers';
$data_string = array(
'access_token' => "<YOUR ACCESS TOKEN>",
'timestamp' => date('Y-m-d H:i:s'),
'name' => "test c",
'company_id' => "<THE COMPANY ID>",
'email' => "[email protected]",
'allow_sending_sms' => "true"
);
$secret_key = "<YOUR SECRET KEY>";
// OpenSSL::HMAC.hexdigest("sha1", @partner.hmac_secret, to_query(canonical_params))
$signature = hash_hmac("sha1", http_build_query($data_string), $secret_key);
$data_string["signature"] = $signature;
$content = json_encode($data_string);
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array('Content-Type:application/json',
'Content-Length: ' . strlen($content))
);
$json_response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($status != 200) {
die("Error: call to URL $url failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($ch);
$response = json_decode($json_response, true);
?>
我已經使用凌空庫 https://developer.android.com/training/volley/index.html
這樣寫Java代碼,但它總是給我一個空體
String URL = "http://developer-api.bringg.com/partner_api/customers";
String date = Utilitty.getDate();
Log.i(TAG, date);
Uri builtUri = Uri.parse("").buildUpon()
.appendQueryParameter("access_token", "accesstoken")
.appendQueryParameter("timestamp", date)
.appendQueryParameter("phone","12889555")
.appendQueryParameter("name", "MAhTest")
.appendQueryParameter("company_id", "id")
.appendQueryParameter("emailz", "[email protected]")
.build();
Log.i(TAG, builtUri.toString().substring(1));
HashMap<String, String> param = new HashMap<>();
param.put("access_token", "accesstoken");
param.put("timestamp", date);
param.put("phone", "12889555");
param.put("name", "MAhTest");
param.put("company_id", "id");
param.put("email", "[email protected]");
String signature = Utilitty.EncodeToken(builtUri.toString().substring(1), "securtykey");
param.put("signature", signature);
Log.i(TAG, signature);
JSONObject jsonObject = new JSONObject(param);
Log.i(TAG, jsonObject.toString());
JsonObjectRequest req = new JsonObjectRequest(URL, jsonObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
VolleyLog.v("Response:%n %s", response.toString(4));
Log.i(TAG + " Response", response.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error 1 : ", error.getMessage());
}
});
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(req);
歡迎來到Stack Overflow!請查看我們的[SO問題清單](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist)來幫助你提出一個好問題,從而得到一個很好的答案。 –
呵呵,@JoeC是絕對正確的。這不是如何工作.. –