Android连接服务器传数据,获得数据

Android连接服务器传数据,获得数据

android连接服务器响应的时候Adroid需要开启线程才能获得响应数据
Activity开启子线程看我的这篇文章https://blog.csdn.net/qq_39582405/article/details/102996697

运行顺序:
1url地址在命令提示符内输入ipconfig可查看
![在这里插入图片描述](https://img-blog.csdnimg.cn/20191110111347877.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM5NTgyNDA1,size_16,color_FFFFFF,t_70Android端
build.gradle导入包
implementation ‘com.google.code.gson:gson:2.7’
implementation ‘com.squareup.okhttp3:okhttp:3.8.1’

Activity调用方法findComment()方法

package com.example.multilevelreviews.dao;

import android.util.Log;
import com.example.multilevelreviews.bean.CommentInfoBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.*;

public class CommentDaoOkhttp {
    private static OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .connectTimeout(10, TimeUnit.SECONDS)
            .writeTimeout(10, TimeUnit.SECONDS)
            .readTimeout(20, TimeUnit.SECONDS)
            .build();
    private static boolean result;
    private static List<CommentInfoBean> lookresult;
    public List<CommentInfoBean> findComment(int videoNo) throws IOException {
        //实体
        List<CommentInfoBean>  commentInfoBeanList = null;
        Gson gson = new Gson();
        //将Activity传输过来的数据转换成gson语言
        String json = gson.toJson(videoNo);
        Log.d("json",json);
        RequestBody responseBody = FormBody.create(MediaType.parse("application/json; charset=utf-8"), json);
        //请求并向服务器传输数据http://192.168.43.86:8080/Bubble/SCommentFindServlet是服务器地址
        Request request = new Request.Builder()
                .url("http://192.168.43.86:8080/Bubble/SCommentFindServlet")
                .post(responseBody)
                .build();
         //获得服务器响应过来的数据
        Response response = okHttpClient.newCall(request).execute();
        String responseData = response.body().string();
        Log.d("responseData",responseData);
        //将服务器传过来的数据转换为List<实体名>类型的数据
        commentInfoBeanList = new Gson().fromJson(responseData,new TypeToken<ArrayList<CommentInfoBean>>(){ }.getType());
        Log.d("commentInfoBeanList",commentInfoBeanList.get(0).getCommentContent());
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
            }
        });
        return commentInfoBeanList;
    }
}

Activity执行以上方法需要开启一个线程来调用这个方法

服务器端
commentList
commentList :是你要传给服务器的数据

import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
import javax.servlet.*;
import com.google.gson.Gson;
import SBusinessService.SCommentService;
import SDomainModel.CommentInfo;
@WebServlet("/SCommentFindServlet")
public class SCommentFindServlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
    private List<CommentInfo> commentList;
    private SCommentService sCommentService = new SCommentService();
     public SCommentFindServlet() {
        super();
     
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
   throws ServletException, IOException {
  Gson gson = new Gson();
  String str = "wwwww!";
  request.setCharacterEncoding("utf-8");
  response.setCharacterEncoding("utf-8");
  //获取客户端传来的数据
  BufferedReader reader = request.getReader();
  String json = reader.readLine();
  System.out.println("json:"+json);
  if(json != null){
   System.out.println("json:"+json);
   //将获得的数据转换成int型
   int videoNo = Integer.parseInt(json);
   //根据videoNo去数据库获得数据并传回来sCommentService.findCommentInfo(videoNo)是我自己的方法,你不用管,你只要把你的数据赋值给commentList就可以了
   commentList = sCommentService.findCommentInfo(videoNo);
   System.out.println("SCommentFindServlet");
   //将数据库传来的数据转换为gson
   str = gson.toJson(commentList);
   System.out.println("Str:"+str);
  } 
  //相应给客户端数据
   response.getWriter().write(str);
  }
  protected void doPost(HttpServletRequest request,
  	 HttpServletResponse response) 
   	throws ServletException, IOException {
   	doGet(request, response);  
 }
} 

  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值