Android 和 Java 使用POST方法连接,传递Json格式的数据

1.下载JsonObject相关jar包

在这里,我们使用JsonObject来将Java类型数据转换成Json类型,首先要下载该库相关的jar包,下载地址如下:
http://files.cnblogs.com/java-pan/lib.rar
2.1 web 端—Class:

@WebServlet("/login")
public class Login extends HttpServlet{
    @Override
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setContentType("application/json; charset=utf-8"); //设置代码返回的编码格式

            String name = request.getParameter("username");
            String pass = request.getParameter("userpass");
            System.out.println("name--"+name);

            JSONObject jsonObject = new JSONObject();   //创建Json对象
            jsonObject.put("login_state", "登录成功");   //设置Json对象的属性
            System.out.println(jsonObject.toString());  //调用toString方法将json对象转换成json字符串

            response.getWriter().write(jsonObject.toString());
        }
}

2.2 Web端——JSP页面

<form action="login" method="post">
        ${tip }<br><br>
        昵称:<input name="username"><br><br>
        密码:<input name="userpass"><br><br>
        <input type="submit"><br>
    form>

3.1 添加okhttpclient包
http://blog.csdn.net/lmj623565791/article/details/47911083;

3.2 Android 端 新建类——HttpUtils

public class HttpUtils {
    final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
    public static OkHttpClient client = new OkHttpClient();

    public static String post(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        String content = response.body().string();
        Log.e("======",content );
        return a;
    }
}

3.3 Android 端

@Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.login_web_btn:

                String name = login_username.getText().toString();
                String pass = login_userpass.getText().toString();

                final String PATH = "http://"+"服务器ip地址"+":8080/LoginProject/login";

                new Thread(){
                    @Override
                    public void run() {
                        try {
                            String content = new HttpUtils().post(PATH,JsonGet(name,pass));
                            ShowToast(content);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
                break;
        }
    }

    private String JsonGet(String name, String pass) {
        JSONObject object = new JSONObject();
        try {
            object.put("name", name);
            object.put("pass", pass);
            return object.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void ShowToast(final String content){  
        runOnUiThread(new Runnable() {         //runOnUiThread方法调用主线程
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(),content,Toast.LENGTH_SHORT).show();
            }
        });
    }

你可能感兴趣的:(java服务端,和,Android交互)