- Get操作(get操作参数是拼接在url后面的)
1 public void doGet(View view) { 2 //1.拿到okHttpClient对象 3 // OkHttpClient okHttpClient = new OkHttpClient(); 4 //2、构造Request 5 Request.Builder builder = new Request.Builder(); 6 Request request = builder 7 .get() 8 .url(mBaseUrl+"login?username=cyd&password=1234") 9 .build(); 10 //3、4 11 executeRequest(request); 12 }
- GAT、POST抽取出来的公共方法
1 public void executeRequest(Request request ){ 2 //3、将Request封装为Call 3 Call call = okHttpClient.newCall(request); 4 //4、执行Call(回调的返回值是response,不在UI线程之内) 5 call.enqueue(new Callback() { 6 @Override 7 public void onFailure(Request request, IOException e) { 8 L.e("onFailure"+e.getMessage()); 9 } 10 @Override 11 public void onResponse(Response response) throws IOException { 12 L.e("onResponse:"); 13 final String str = response.body().string(); 14 // textView.setText(str);不能在子线程操作UI 15 //正确方法: 16 runOnUiThread(new Runnable() { 17 @Override 18 public void run() { 19 textView.setText(str); 20 } 21 }); 22 } 23 }); 24 25 }
- Post(post参数是构造一个请求体responseBody)
1 public void doPost(View view) { 2 //1.拿到okHttpClient对象 3 //2、构造Request 4 //2.1、构造requestBody 5 //FormEncodingBuilder是传递post的参数的builder 6 FormEncodingBuilder requestBodyBuilder = new FormEncodingBuilder(); 7 RequestBody requestBody = requestBodyBuilder 8 .add("username","cyd") 9 .add("password","123456789").build(); 10 11 Request.Builder builder = new Request.Builder(); 12 Request request = builder.url(mBaseUrl + "login").post(requestBody).build(); 13 //3、4 14 executeRequest(request); 15 }
- POST一个JSON字符串
1 public void doPostString(View view) { 2 //将String作为requestBody发送出去,与上面不同的只有requestBody的构造方法 3 //仅仅post字符串不需要使用FormEncodingBuilder的构造者模式 4 5 //第一个参数包含类型和字符编码,第二个是字符串 6 RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain;chaset=utf-8"), "{username:cyd,password:09876}"); 7 Request.Builder builder = new Request.Builder(); 8 Request request = builder.url(mBaseUrl + "postString").post(requestBody).build(); 9 //3、4 10 executeRequest(request); 11 }
服务端打印出字符串结果:
- 服务端代码:
1 public String login() { 2 System.out.println(username+","+password); 3 HttpServletResponse response = ServletActionContext.getResponse(); 4 try { 5 PrintWriter writer = response.getWriter(); 6 writer.write("login success!"); 7 writer.flush(); 8 } catch (IOException e) { 9 // TODO Auto-generated catch block 10 e.printStackTrace(); 11 } 12 return null; 13 } 14 public String postString() { 15 HttpServletRequest request = ServletActionContext.getRequest(); 16 try { 17 ServletInputStream iServletInputStream = request.getInputStream(); 18 19 StringBuilder sb = new StringBuilder(); 20 int len =0; 21 byte[] buf = new byte[1024]; 22 while((len = iServletInputStream.read(buf))!= -1) { 23 sb.append(new String (buf,0 ,len)); 24 } 25 System.out.println(sb.toString()); 26 27 } catch (IOException e) { 28 // TODO Auto-generated catch block 29 e.printStackTrace(); 30 } 31 32 return null; 33 }
- 服务端接收参数是通过成员变量获取,客户端接收到服务端的数据一般是通过回调里面的response拿到inputStream,服务端获取客户端的数据流是通过request拿到的
- session保持问题:用户登录后服务端会产生一个sessionID,sessionID会以cookie的形式返回给客户端,客户端拿到sessionID后若服务端sessionID还在生命周期中则可以不用重复登录,可以作为判断用户是否登录。可以获取sessionID
- 笔记来源于:https://www.imooc.com/learn/764