java - 将mysql数据从servlet发送到Android
问题描述
我正在尝试使用 JSON 将我的 mysql 数据从 servlet 发送到 android 应用程序,但它显示错误java.net.connectException connection timed out
这是带有recycler View的android Fragment。RecyclerView 在数据为虚拟时工作正常
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_homefrag, container, false);
final Context context=v.getContext();
recyclerView= v.findViewById(R.id.recyler);
final List<UserRecycle> sample=new ArrayList<>();
final String JSON_URL = "http://10.0.2.2:8080/OnlineBookStore/Home";
RequestQueue requestQueue= Volley.newRequestQueue(context);
StringRequest stringRequest=new StringRequest(Request.Method.GET, JSON_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject jsonObject=new JSONObject(response);
JSONArray bookArray=jsonObject.getJSONArray("BookData");
for(int i=0;i<bookArray.length();i++){
JSONObject bookobject=bookArray.getJSONObject(i);
UserRecycle books=new UserRecycle();
book_ids.add(bookobject.getString("id"));
books.b_name=bookobject.getString("bname");
books.b_author=bookobject.getString("bauthor");
books.b_price=bookobject.getString("bprice");
books.b_avail=bookobject.getString("bstock");
books.b_image=R.drawable.bbanim;
sample.add(books);
}
layoutManager=new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayout.VERTICAL));
recyclerView.setAdapter(new RecyclerAdapter(sample));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getContext(), "error "+error.getMessage(), Toast.LENGTH_LONG).show();
}
});
stringRequest.setRetryPolicy(new DefaultRetryPolicy( 50000, 5, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(stringRequest);
return v;
}
这是我的 Servlet 类
public class Home extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection con=Connect.connect();
try{
PrintWriter out=response.getWriter();
PreparedStatement pst=con.prepareStatement("select * from book order by b_id desc;");
ResultSet rst=pst.executeQuery();
JsonObject jsonResponse= new JsonObject();
JsonArray data = new JsonArray();
while(rst.next()){
JsonObject row=new JsonObject();
row.addProperty("id", rst.getString(1));
row.addProperty("bname", rst.getString(2));
row.addProperty("bauthor", rst.getString(3));
row.addProperty("bprice", rst.getString(4));
row.addProperty("bstock", rst.getString(5));
data.add(row);
}
jsonResponse.add("BookData", data);
out.print(jsonResponse);
}
catch(SQLException e){
}
}
}
当我在网页中打印数据时,它工作正常。
我想问题在于在 Servlet 类或 url 中创建响应。可以的话请帮忙谢谢。
解决方案
推荐阅读
- github - GitHub:我们还没有找到这个存储库的任何依赖项
- c - 我想解决二进制信号量的读者/作者问题
- android - android studio中的XML文件行给出错误它说URI未注册
- google-maps - 谷歌地图全屏在特殊情况下不显示
- django - 元素可用于多于一个的组
- vb.net - 使用 Flatten 函数画圆
- url - 在 TYPO3 9 中禁用路由
- java - Java中使用Kadane算法的子数组的最大总和
- java - 如何使用 JAXB 从 nexus 存储库生成 java 类
- react-native - React-Native-Numeric-Input 中的值没有变化