如何使用StringRequest在JSON中发送POST请求

时间:2015-10-17 16:23:35

标签: java android json post jersey

我正在尝试在Json中使用StringRequest发送帖子请求。我在java中设置了Jersey Servlet并在android studio中创建了android项目。 我可以在Chrome中使用Poster Plugin发送json对象,但我无法安装android客户端。

请帮帮我。

Jersey Servlet:

@Path("/register")
public class RegisterServlet {

    @POST
    @Path("/add")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response addUser(UserDTO user){

        if((user.getUsername()=="") || (user.getEmail()=="") || (user.getPassword()=="")){
            Logger.getLogger(RegisterServlet.class).info("Name or Email or Password is empty");
            return Response.status(200).entity("empty").build();
        }

        if(Service.getInstance().addUser(user)){
            Logger.getLogger(RegisterServlet.class).info("Added User --> username: "+user.getUsername()+"email: "+user.getEmail());
            return Response.status(200).entity("add").build();
        }else{
            Logger.getLogger(RegisterServlet.class).info("Error Add User");
            return Response.status(200).entity("error").build();
        }
    }
}

Android代码;

private void postUser(final String username,final String email,
                        final String password,final String dep_name){
    String tag_string_req = "req_register";

    StringRequest strReq = new StringRequest(Method.POST,
            "http://192.168.56.1:8080/ServerApp/register/add", new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            hideDialog();

            try {
                JSONObject jObj = new JSONObject(response);

            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_LONG).show();
            hideDialog();
        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            // Posting params to register url
            Map<String, String> params = new HashMap<String, String>();
            params.put("name", username);
            params.put("email", email);
            params.put("password", password);
            params.put("dep_name", dep_name);

            return params;
        }

    };

    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);

}

Android中的AppController;

public class AppController extends Application {

    public static final String TAG = AppController.class.getSimpleName();

    private RequestQueue mRequestQueue;

    private static AppController mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized AppController getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }
}

0 个答案:

没有答案