3

ジャージPOSTアノテーションでPOSTメソッド(Android)によって送信されたデータにアクセスするにはどうすればよいですか? Androidクライアント:

public class TestClient extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_main);
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("myURL");

        try {

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("user", "user1"));
            nameValuePairs.add(new BasicNameValuePair("password", "password1"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            httpclient.execute(httppost);

        } catch (ClientProtocolException e) {

        } catch (IOException e) {

        }

    }

}

以下は、Tomcatによってローカルホスト上で実行されるサーバーのコードです。

@Path("test")
public class Test {
     @GET
     @Produces(MediaType.TEXT_PLAIN)
        public String respondAsReady() {
            return "Running";
        }

     @POST
     @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
     @Produces(MediaType.TEXT_PLAIN or other?)

              //Here I'd like the program to post (with System.out.println) the data to the console which is uploaded by the client using the POST method.


        }
}
4

1 に答える 1

1

クライアントがPHP、html、またはhttpclientを使用するandroidであるかどうかに関係なく、サーバー側は気にしないでください。@FormParamアノテーションを使用して値を取得できるはずです。コメントに投稿した質問の回答、またはこの例を参照してください。

あなたはあなたの答えを次のようなもっと単純なものに変えることができるはずです:

@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void themethodname(@FormParam("user") String user, 
                          @FormParam("password") String password) {
    System.out.println("Username: "+user+", "+"Password: "+password);
}
于 2012-10-08T17:46:12.740 に答える