我要創建Android應用程序這個職位的要求,並得到JSON響應:重新編寫PHP POST請求到Java andoird POST請求(並得到響應)
function send_post_to_url($url,$post) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$return = curl_exec($ch);
curl_close($ch);
return $return;
}
$data['api_key'] = YOUR_API_KEY;
$data['action'] = 'TestFunction';
$response = send_post_to_url('https://api.superget.co.il/',$data);
echo $response;
我怎樣才能做到這一點?
我曾嘗試以下,但連接不斷trowing異常
HttpURLConnection urlConnection;
urlConnection = (HttpURLConnection) ((new URL("https://api.superget.co.il").openConnection()));
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestMethod("POST");
urlConnection.connect();
嘗試使用Android例如任何圖書館http://square.github.io/okhttp/ HTTPS: //raw.githubusercontent.com/square/okhttp/master/samples/guide/src/main/java/okhttp3/guide/PostExample.java –