How can I access the data which is sent by POST method (Android) in the jersey POST annotation?

How can I access the data which is sent by POST method (Android) in the jersey POST annotation? Android Client:

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) {

        }

    }

}

The following is the code of the Server which is run on Localhost by 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.


        }
}

Upvotes: 3

Views: 457

Answers (1)

digitaljoel
digitaljoel

Reputation: 26574

Regardless of whether the client is PHP, html, or android using httpclient the server side shouldn't care. You should be able to use the @FormParam annotation to get the values. See the answer in the question I posted in the comments, or this example.

You should be able to change your answer to something simpler like :

@POST
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void themethodname(@FormParam("user") String user, 
                          @FormParam("password") String password) {
    System.out.println("Username: "+user+", "+"Password: "+password);
}

Upvotes: 1

Related Questions