1
implementation ‘com.squareup.okhttp3:okhttp:3.9.0’
2 使用get 同步和异步
/**
* get 异步Get
*/
public void aSyncGet(String url) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
}
@Override
public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
String result = response.body().string().toString();
Log.i("tag", "请求成功: " + result);
}
});
}
/**
* get 同步Get
*/
public void aSyncGet(String url) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();//得到Response 对象
if (response.isSuccessful()) {
Log.d("kwwl","res=="+response.body().string());
}
2 使用post 同步和异步 服务器必须是支持json, 服务器如果是form表单用下面的这种。
/**
* 异步Post
*
* @param url url
* @return
*/
public void loginaSyncPost(String url,String username,String pwd) {
Map map = new HashMap();
map.put("pwd", pwd);
map.put("username",username);
JSONObject json =new JSONObject(map);
String a = json.toString().trim();
OkHttpClient client = new OkHttpClient();
MediaType JSON = MediaType.parse("application/json;charset=UTF-8");
RequestBody body = RequestBody.create(JSON, a);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
try {
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String result = response.body().string().toString();
Log.i("tag", a+url+"请求成功: " + result);
try {
JSONObject jsonObject =new JSONObject(result);
String code =jsonObject.getString("code");
JSONObject datajson = (JSONObject) jsonObject.get("data");
String token =datajson.getString("token");
editor.putString("token",token).commit();
if(code.equals("200")){
runOnUiThread(new Runnable() {
@Override
public void run() {
Intent intent = new Intent(LoginActivity.this, BleActivity.class);//BottomTabLayoutActivity BleActivity
startActivity(intent);
finish();
}
});
}
} catch (Exception e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
}
});
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 同步Post
*
* @param url url
* @return
*/
public void loginSyncPost(String url,String username,String pwd) {
Map map = new HashMap();
map.put("pwd", pwd);
map.put("username",username);
JSONObject json =new JSONObject(map);
String a = json.toString().trim();
OkHttpClient client = new OkHttpClient();
MediaType JSON = MediaType.parse("application/json;charset=UTF-8");
RequestBody body = RequestBody.create(JSON, a);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
try {
Response response = client.newCall(request).execute();
String res = response.body().string();
} catch (Exception e) {
e.printStackTrace();
}
}
3 异步 post ,form表单
/**
* get 异步Post同求
*
* @param url url
* @return
*/
public void nonSyncPostFormBody(String url) {
OkHttpClient client = new OkHttpClient();
FormBody.Builder formBody = new FormBody.Builder();//创建表单请求体
formBody.add("authCode","388091");//传递键值对参数
formBody.add("phone","18319030295");//传递键值对参数
formBody.add("username","18319030295");//传递键值对参数
formBody.add("sourceType","3");//传递键值对参数
formBody.add("pwd","123456");//传递键值对参数
Request request = new Request.Builder()
.url(url)
.post(formBody.build())
.build();
try {
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String result = response.body().string().toString();
Log.i("tag", "请求成功: " + result);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}