今日学习:OKhttp和异步任务
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