フォーム入力から取得した投稿パラメーターを使用してhttp投稿リクエストを送信する簡単なコード例が必要です。私はApache HTTPClientを見つけました。非常にリーチなAPIと多くの洗練された例がありますが、入力パラメーターを使用してhttp post要求を送信し、テキスト応答を取得する簡単な例は見つかりませんでした。
pdate: 3.xは非推奨であるため、Apache HTTPClient v.4.xに興味があります。
HTTP POST Apache HttpClientを使用したリクエストの例v.4.x
HttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody("param1", param1Value, ContentType.TEXT_PLAIN);
builder.addTextBody("param2", param2Value, ContentType.TEXT_PLAIN);
HttpEntity multipart = builder.build();
httpPost.setEntity(multipart);
HttpResponse response = httpClient.execute(httpMethod);
Apache HTTPClient APIを使用したHttp POSTのサンプルコードを次に示します。
import Java.io.InputStream;
import org.Apache.commons.httpclient.HttpClient;
import org.Apache.commons.httpclient.methods.PostMethod;
public class PostExample {
public static void main(String[] args){
String url = "http://www.google.com";
InputStream in = null;
try {
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(url);
//Add any parameter if u want to send it with Post req.
method.addParameter("p", "Apple");
int statusCode = client.executeMethod(method);
if (statusCode != -1) {
in = method.getResponseBodyAsStream();
}
System.out.println(in);
} catch (Exception e) {
e.printStackTrace();
}
}
}
このコードを、アプリケーションで使用したAndroid Andrew Gertigによるプロジェクトからプルしました。これにより、HTTPostを実行できます。時間があれば、POJOの例を作成しますが、うまくいけば、コードを分析して必要なものを見つけることができます。
アルシャク
https://github.com/AndrewGertig/RubyDroid/blob/master/src/com/gertig/rubydroid/AddEventView.Java
private void postEvents()
{
DefaultHttpClient client = new DefaultHttpClient();
/** FOR LOCAL DEV HttpPost post = new HttpPost("http://192.168.0.186:3000/events"); //works with and without "/create" on the end */
HttpPost post = new HttpPost("http://cold-leaf-59.heroku.com/myevents");
JSONObject holder = new JSONObject();
JSONObject eventObj = new JSONObject();
Double budgetVal = 99.9;
budgetVal = Double.parseDouble(eventBudgetView.getText().toString());
try {
eventObj.put("budget", budgetVal);
eventObj.put("name", eventNameView.getText().toString());
holder.put("myevent", eventObj);
Log.e("Event JSON", "Event JSON = "+ holder.toString());
StringEntity se = new StringEntity(holder.toString());
post.setEntity(se);
post.setHeader("Content-Type","application/json");
} catch (UnsupportedEncodingException e) {
Log.e("Error",""+e);
e.printStackTrace();
} catch (JSONException js) {
js.printStackTrace();
}
HttpResponse response = null;
try {
response = client.execute(post);
} catch (ClientProtocolException e) {
e.printStackTrace();
Log.e("ClientProtocol",""+e);
} catch (IOException e) {
e.printStackTrace();
Log.e("IO",""+e);
}
HttpEntity entity = response.getEntity();
if (entity != null) {
try {
entity.consumeContent();
} catch (IOException e) {
Log.e("IO E",""+e);
e.printStackTrace();
}
}
Toast.makeText(this, "Your post was successfully uploaded", Toast.LENGTH_LONG).show();
}
http://httpunit.sourceforge.net/doc/cookbook.html PostMethodWebRequestおよびsetParameterメソッドを使用
は、Htmlページから投稿する非常に単純な例を示しています。サーブレットはそれを処理し、テキスト応答を送信します。
http://Java.Sun.com/developer/onlineTraining/Programming/BasicJava1/servlet.html