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

今日学习:OKhttp和异步任务

程序员文章站 2022-03-08 15:31:58
//subclass of getRemoteIPprivate class getRemoteIP extends AsyncTask {protected String doInBackground(Object[] objects) {String myStr="";try {OkHttpClient client=new OkHttpClient();Request request=new Request.Builder().url...

OkHttp需要导入一个依赖包。地址是com.squareup.okhttp,artifact的ID是okhttp

//subclass of getRemoteIP
private class getRemoteIP extends AsyncTask<Object,Object,String> {
protected String doInBackground(Object[] objects) {
String myStr="";
try {
OkHttpClient client=new OkHttpClient();
Request request=new Request.Builder().url(“webaddress/getMyIPJSON”).build();
Response response=client.newCall(request).execute();
if(response.isSuccessful()){
myStr =response.body().string();
try {
JSONObject obj = new JSONObject(myStr);
myStr=obj.get(“ip”).toString() ;
} catch (JSONException e) {
e.printStackTrace();
}
}
else{
myStr = “remote access is not reachable”;
}
} catch (IOException e) {
e.printStackTrace();
}
return myStr;
}
android Studio有时候重启启动APP不生效。必须按红色停止才能生效新代码。想到以前放弃android就是因为编译卡。现在编译不怎么卡了,可是有这个问题。不过也没什么大不了的,从日志和行为里就可以看出来是不是生效了。

本文地址:https://blog.csdn.net/ilife007/article/details/112911826

相关标签: android