zoukankan      html  css  js  c++  java
  • 毕业设计-进度-客户端向服务器传输数据2.3

    昨天的问题

    手机无法通过网络用socket连接服务器

    改为使用其他的端口,设置本地计算机为服务器均无法解决问题,在设置本地计算机为服务器时还出现了百度地图api连接异常的问题,显示AK鉴权失败,最终采用http协议代替套接字

    通过http协议连接客户端与服务器端

    Get

    客户端

     public void run() {
                            HttpPost httpRequest = new HttpPost(http://172.16.99.207:8080/AndroidServer/AndroidServerServlet);
                            List<namevaluepair> params = new ArrayList<namevaluepair>();
                            params.add(new BasicNameValuePair(clientData, 您好服务器端!));
                            try {
                                Message message = new Message();
                                Bundle bundle = new Bundle();
                                httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));//设置请求参数项
                                HttpClient httpClient = new DefaultHttpClient();
                                HttpResponse httpResponse = httpClient.execute(httpRequest);//执行请求返回响应
                                if(httpResponse.getStatusLine().getStatusCode() == 200){//判断是否请求成功
                                    bundle.putString(msg, EntityUtils.toString(httpResponse.getEntity()));
                                }else{
                                    bundle.putString(msg, 没有获取到Android服务器端的响应!);
                                }
                                message.setData(bundle);
                                handler.sendMessage(message);
                            } catch (ClientProtocolException e) {
                                e.printStackTrace();
                            } catch (UnsupportedEncodingException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                }
    

    服务器端

    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        response.setContentType(text/plain; charset=UTF-8);
        request.setCharacterEncoding(UTF-8);
        System.err.println(request.getParameter(clientData));
        PrintWriter printWriter = response.getWriter();
        ;
        printWriter.flush();
        printWriter.close();
         }
     }
    

    待完成

  • 相关阅读:
    Android-实现切换Fragment页功能
    Mybatis中insert返回主键ID
    ScrollView 与ListView 滑动冲突完美解决
    fresco的源码学习自我总结
    React-Native错误笔记-EPERM
    React-Native的基本控件属性方法
    React Native之ViewPagerAndroid跳转页面问题
    Android应用中-更新提示显示红点的方案
    关于GrideView Item点击后出现错乱重叠的情况
    分享一个Unity3D小作品,源码地址已公布在文章开头!
  • 原文地址:https://www.cnblogs.com/1605-3QYL/p/12261683.html
Copyright © 2011-2022 走看看