// post请求,以json形式封装请求参数放在请求体中。响应是json,以指定类的实例对象返回请求结果 private void postJsonBody() { LoginAccount la = new LoginAccount("zhangsa", "123456"); String url = "http://192.168.1.102:8080/Test/HelloServlet"; RequestInfo requestInfo = new RequestInfo(); requestInfo.url = url; requestInfo.contentType = RequestInfo.ContentType.CT_JSON; requestInfo.bodyContent = JSON.toJSONString(la); // 将LoginAccount实例属性,转成json字符串 afeitaNet.postJson( requestInfo, new NetCallback<UserInfo>() { @Override public void onResult(UserInfo response) { Toast.makeText( NetSampleActivity.this, "get HelloServlet post 调用成功:" + response, Toast.LENGTH_LONG) .show(); } }, UserInfo.class); }
// post请求带入参响应是json,以指定类的实例对象返回请求结果 private void postJson() { String url = "http://192.168.1.102:8080/Test/HelloServlet"; RequestInfo requestInfo = new RequestInfo(); requestInfo.url = url; requestInfo.setParams("username", "zhangsa"); requestInfo.setParams("password", "123456"); requestInfo.isShowLoadingDialog = false; afeitaNet.postJson( requestInfo, new TipsingNetCallback<UserInfo>(this) { @Override public void onResult(UserInfo response) { Toast.makeText( NetSampleActivity.this, "get HelloServlet post 调用成功:" + response, Toast.LENGTH_LONG) .show(); } }, UserInfo.class); }