Android连接服务器传数据,获得数据
android连接服务器响应的时候Adroid需要开启线程才能获得响应数据
Activity开启子线程看我的这篇文章https://blog.csdn.net/qq_39582405/article/details/102996697
运行顺序:
1url地址在命令提示符内输入ipconfig可查看
Android端
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);
}
}