次のコードを使用すると、データを取得できます。
private static String getHttpPostContent(String address, String token) {
try {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(address);
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("token", token));
post.setEntity(new UrlEncodedFormEntity(pairs));
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
String serverResponse = EntityUtils.toString(entity);
return serverResponse;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
私のドキュメントに基づいて、エラーが発生した場合、サーバーはエラー 418 を送信して、何か問題が発生したことを通知します。捕まえ方がわからない?
HTTP GET メソッドを使用している場合、次のように応答コードを確認する方法があります。
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK)
...
HTTP POST メソッドに同様の方法はありますか?