欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android使用okHttp(get方式)登录

程序员文章站 2024-03-06 12:45:14
本文实例为大家分享了android使用get方式登录的相关代码,供大家参考,具体内容如下 工具类  package com.liunan.okht...

本文实例为大家分享了android使用get方式登录的相关代码,供大家参考,具体内容如下

工具类 

package com.liunan.okhttpdemo3post.utils;

import java.io.ioexception;

import okhttp3.mediatype;
import okhttp3.okhttpclient;
import okhttp3.request;
import okhttp3.requestbody;
import okhttp3.response;
import okhttp3.responsebody;

/**
 * created by administrator on 2016-03-27.
 */
public class httputils {

  okhttpclient client = new okhttpclient();
  public static final mediatype json
      = mediatype.parse("application/json; charset=utf-8");

  public string login(string url, string json) throws ioexception {
    //把请求的内容字符串转换为json
    requestbody body = requestbody.create(json, json);
    //requestbody formbody = new formencodingbuilder()

    request request = new request.builder()
        .url(url)
        .post(body)
        .build();

    response response = client.newcall(request).execute();

    string result = response.body().string();


    return result;


  }


  public string bolwingjson(string username, string password) {
    return "{'username':" + username + "," + "'password':" + password + "}";
    //   "{'username':" + username + ","+"'password':"+password+"}";
  }


} 

activity

package com.liunan.okhttpdemo3post;

import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.text.textutils;
import android.util.log;
import android.view.view;
import android.widget.button;
import android.widget.edittext;
import android.widget.textview;
import android.widget.toast;

import com.liunan.okhttpdemo3post.utils.httputils;

import org.w3c.dom.text;

import java.io.ioexception;

public class mainactivity extends appcompatactivity implements view.onclicklistener {
  private static final string tag ="mainactivity" ;
  //用户名
  private edittext metusername;
  //密码
  private edittext metpwd;
  //登录按键
  private button mbtnlogin;
  private textview mtvresult;

  private string url ="http://192.168.1.102:8080/login/login";

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);

    initview();
    initlistener();
  }

  /**
   * 初始化组件
   */
  private void initview() {

    metusername = (edittext) findviewbyid(r.id.login_et_name);
    metpwd = (edittext) findviewbyid(r.id.login_et_pwd);

    mbtnlogin = (button) findviewbyid(r.id.login_btn_login);


    mtvresult = (textview) findviewbyid(r.id.login_tv_result);

  }

  /**
   * 设置监听器
   */
  private void initlistener() {

    mbtnlogin.setonclicklistener(this);


  }

  /*
  单击事件监听
   */
  @override
  public void onclick(view v) {
    if(v==mbtnlogin){
      login();
    }
  }

  /*
  登录
   */
  private void login() {

    final string username = metusername.gettext().tostring().trim();
    final string password = metpwd.gettext().tostring().trim();


    if(textutils.isempty(username) || textutils.isempty(password)){

      toast.maketext(mainactivity.this, "用户名或者密码不能为空", toast.length_short).show();
      return;
    }

    new thread(){
      @override
      public void run() {

          httputils httputils = new httputils();
          //转换为json
          string user = httputils.bolwingjson(username, password);



        //string user ="{'username':" + username + ","+"'password':"+password+"}";

        log.d(tag, "user:" + user);

        try {
          final string result = httputils.login(url, user);
          log.d(tag, "结果:" + result);
          //更新ui,在ui线程中
          runonuithread(new runnable() {
            @override
            public void run() {
              if("success".equals(result)){

                mtvresult.settext("登录成功");

              }else{
                mtvresult.settext("登录失败");
              }
            }
          });
        } catch (ioexception e) {
          e.printstacktrace();
        }




      }
    }.start();


  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。