开发中遇到用okhttp 请求获取数据,刚做完一点,过程中遇到过一些坑,不过现在都解决了,再次记录一下,直接上代码:
get方式的请求
new Thread() {
@Override
public void run() {
OkHttpClient okHttpClient = new OkHttpClient();
String format = String.format("ip地址加参数拼接");
//类似 KeyPath.Path.head + KeyPath.Path.smsalarm, username, userPass, type, lat, lon, finalOptions, text10 KeyPath.Path.head + KeyPath.Path.smsalarm是封装好的ip地址 后面是参数
Request build1 = new Request.Builder().url(format).build();
okHttpClient.newCall(build1).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
if (string != null) {
try {
final JSONObject jsonObject = new JSONObject(string);
int status = jsonObject.getInt("status");
if (status == 0) {//接口返回正确
mHandler.post(new Runnable() {
@Override
public void run() {
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
});
}
}.start();
post请求
new Thread() {
@Override
public void run() {
OkHttpClient okHttpClient = new OkHttpClient();
FormBody formBody = new FormBody.Builder()
//add要拼接的form表单
.add("carnumber", text1)
.build();
String format = String.format("ip地址拼接+参数");
//类似 KeyPath.Path.head + KeyPath.Path.smsalarm, username, userPass, type, lat, lon, finalOptions, text10 KeyPath.Path.head + KeyPath.Path.smsalarm是封装好的ip地址 后面是参数
Request request = new Request.Builder()
.url(format)
.post(formBody)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
//"数据获取失败,请重新尝试!"
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
if (string != null) {
try {
JSONObject jsonObject = new JSONObject(string);
int status = jsonObject.getInt("status");
if (status == 1) {
mHandler.post(new Runnable() {
@Override
public void run() {
// "发送成功!"
}
});
} else {
mHandler.post(new Runnable() {
@Override
public void run() {
// "发送失败,请稍后重试!
}
});
}
} catch (JSONException e) {
}
}
}
});
}
}.start();
put请求
new Thread() {
@Override
public void run() {
OkHttpClient okHttpClient = new OkHttpClient();
FormBody build = new FormBody.Builder()
.add("Rfid", Rfid)
.add("state", "1")
.add("u", username)
.add("key", key)
.add("utc", current_timestamp)
.build();
String format = String.format("http://192.168.1.76:9299/api/iceplate/setstate/%s?state=%s&api_key=&api_key=&u=%s&key=%s&utc=%s",Rfid, 1, username, key, current_timestamp);
Request build1 = new Request.Builder()
.url(format)
.put(build)
.build();
okHttpClient.newCall(build1).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
if (string != null) {
try {
final JSONObject jsonObject = new JSONObject(string);
int status = jsonObject.getInt("status");
if (status == 0) {
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(Home_Thelibrary.this, "修改状态成功!", Toast.LENGTH_SHORT).show();
show();
}
});
}else {
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(Home_Thelibrary.this, "修改状态失败,请稍后重试!", Toast.LENGTH_SHORT).show();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
} finally {
progressDlgEx.closeHandleThread();
}
}
}
});
}
}.start();
这只是本人开发过程中自己琢磨和学习的方式,如果有不对的地方,欢迎指正。