我用這個來測試dmy,身體是JSON格式的。所以請將您的Student對象轉換爲JSON數據。
public String post(String url, HashMap<String, String>params, String body) throws Exception {
HttpPost postRequest = new HttpPost(url);
for(String key : params.keySet()){
postRequest.addHeader(key, params.get(key));
}
StringEntity input = new StringEntity(body);
input.setContentType("application/json");
postRequest.setEntity(input);
HttpResponse response = (new DefaultHttpClient()).execute(postRequest);
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatusLine().getStatusCode());
}
BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));
String output;
StringBuffer totalOutput = new StringBuffer();
while ((output = br.readLine()) != null) {
totalOutput.append(output);
}
return totalOutput.toString();
}