zoukankan      html  css  js  c++  java
  • 每日总结

      对于OK HTTP的使用,在网上找到了多篇教程,经过对其进行理解,找到了适合我当前使用的方法。使用javaweb的开发模式,设计出servlet层,手机端向servlet发送请求,然后servlet做出相应的处理在向手机端发送信息。对于这种开发模式进行代码的编写,找到了合适的例子:

    在服务器端,现在没有租用服务器,就使用自己的电脑了,首先在电脑上新建一个web项目,然后新建一个servlet,部署Tomcat,然后对servlet进行编写,现在只是实现简单测试案例,测试可行性,

    测试用到的代码如下:

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //super.doGet(req, resp);
    resp.setContentType("text/html;charset=utf-8");
    resp.setHeader("content-type", "text/html;charset=utf-8");
    String username = req.getParameter("username");
    String password = req.getParameter("password");
    System.out.println(username);
    System.out.println(password);
    if (username.equals("ycy")&password.equals("123")){
    resp.getWriter().print("登陆成功");
    }else {
    resp.getWriter().print("登陆失败");
    }

    这是servlet的代码,用于接收手机端传送来的数据,并且根据输入情况向手机端传回相应信息。

    手机端代码:

    public class MainActivity extends AppCompatActivity {
    private EditText username;
    private EditText pwd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    username = findViewById(R.id.username);
    pwd = findViewById(R.id.password);
    }
    //Get请求
    public void LoginGet(View view){
    String name=username.getText().toString();
    String password=pwd.getText().toString();
    String path="http://:8080/ceshi/servlet?username="+name+"&password="+password;
    System.out.println(path);
    System.out.println("用户名:"+name);
    System.out.println("密码:"+password);

    OkHttpClient client =new OkHttpClient();
    Request request=new Request.Builder().url(path).get().build();
    client.newCall(request).enqueue(new Callback() {
    //失败
    @Override
    public void onFailure(Call call, IOException e) {
    Log.d("MainActivity","连接失败"+e.getLocalizedMessage());
    }
    //成功
    @Override
    public void onResponse(Call call, Response response) throws IOException {
    String result = response.body().string();
    Log.d("MainActivity",result);
    if (response.body()!=null){
    response.body().close();
    }
    }
    });

    }
    //Post请求
    public void LoginPost(View view){
    String name=username.getText().toString();
    String password=pwd.getText().toString();
    String url="http://192.168.43.227:8080/demo_android/login.action";

    OkHttpClient client=new OkHttpClient();
    //构建表单参数
    FormBody.Builder requestBuild=new FormBody.Builder();
    //添加请求体
    RequestBody requestBody=requestBuild
    .add("username",name)
    .add("password",password)
    .build();

    Request request=new Request.Builder()
    .url(url)
    .post(requestBody)
    .build();
    System.out.println(request.toString());
    //异步请求
    client.newCall(request).enqueue(new Callback() {
    @Override
    public void onFailure(Call call, IOException e) {
    Log.d("MainActivityPost","连接失败"+e.getLocalizedMessage());
    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {
    String result = response.body().string();
    Log.d("MainActivity",result);
    if (response.body()!=null){
    response.body().close();
    }
    }

    });
    }

    完成这一步的时候,需要用电脑连接手机热点,使手机和电脑处于同一网络下,以此来实现通信,同时请求的地址要换成电脑的IP地址。

    经过测试,果然可以传递信息,但这其中也有一些细节,明天在进行细节的解释。

  • 相关阅读:
    Redis企业级数据备份与恢复方案
    使用canal增量同步mysql数据库信息到ElasticSearch
    SpringBoot基于数据库实现简单的分布式锁
    SpringBoot+ShardingSphere实现分库分表 + 读写分离
    SpringBoot 使用JestClient操作Elasticsearch
    Java 操作 MongoDB
    VS C#开发中WinForm中Setting.settings的作用
    Sql 触发器禁用和启用
    ROW_NUMBER over (order by **)
    Aspen 安装
  • 原文地址:https://www.cnblogs.com/ruangongwangxiansheng/p/14911356.html
Copyright © 2011-2022 走看看