• 毕业设计-进度-客户端向服务器传输数据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();
         }
     }
    

    待完成

  • 相关阅读:
    founder面试题
    项目bug的修正
    Linux下分割、合并PDF(pdftk),用于Linux系统的6款最佳PDF页面裁剪工具
    Vim global命令和重复操作
    嵌入式linux GUI--DirectFB + GTK至尊秘笈
    让QT/Embedded支持国际化
    开篇-QT完全手册
    java多线程样例
    Windows Minifilter驱动
    poj 3735 大数量反复操作问题(矩阵高速幂)
  • 原文地址:https://www.cnblogs.com/1605-3QYL/p/12261683.html
Copyright © 2020-2023  润新知