首页 技术 正文
技术 2022年11月8日
0 收藏 825 点赞 1,232 浏览 2939 个字

工具类

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(); }
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,991
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,506
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,349
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,134
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,766
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,844