【发布时间】:2019-03-23 20:54:36
【问题描述】:
我在活动中创建了 okhttp3 方法并且效果很好,但是我想从另一个活动中调用这个 okhttp3 方法,所以我相信我需要通过创建单独的类来分离这个 okhttp3,所以我需要帮助如何创建单独的 okhttp3 类
这是我在活动中创建的方法
public void getJsonData(final int method, final String email, final String password, final String page, final String token){
OkHttpClient client = new OkHttpClient();
Request request;
RequestBody formBody;
HttpUrl gurl;
//mToken = token;
if(networkIsAvailable()){
if(method == POST){
mProgressBar.setVisibility(View.VISIBLE);
String url = "http://35.240.138.251/api/auth/login";
formBody = new FormBody.Builder()
.add("email", email)
.add("password", password)
.build();
request = new Request.Builder()
.url(url)
.post(formBody)
.build();
} else {
gurl = new HttpUrl.Builder()
.scheme("http")
.host("35.240.138.251")
.addPathSegment("api")
.addPathSegment(page)
.addQueryParameter("token",token)
.build();
request = new Request.Builder()
.url(gurl)
.build();
}
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
if(method == POST){
runOnUiThread(new Runnable() {
@Override
public void run() {
//Toast.makeText(getApplicationContext(),"Gangguan koneksi, periksa koneksi internet anda!",Toast.LENGTH_LONG).show();
SHOULD WE MOVE THIS --> errorAlert(); <--
--> login(); <--
}
});
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
String intoken = mJsonManager.getToken();
if(!nager.isEmpty()) {
getJsonData(GET,null,null,"wartarutin",intoken);
} else {
getJsonData(GET,null,null,"wartaprofil",intoken);
}
}
});
}
}
@Override
public void onResponse(Call call, Response response) throws IOException {
if(response.code() == 401){
if(method == POST) {
runOnUiThread(new Runnable() {
@Override
public void run() {
--> reLoginAlert(); <--
}
});
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
getJsonData(POST,email,password,null,null);
}
});
}
}
if(response.isSuccessful()){
if(method == POST) {
try {
String jsonData = response.body().string();
mJsonManager.setToken(jsonData);
Log.v(TAG, jsonData);
runOnUiThread(new Runnable() {
@Override
public void run() {
-- > loginArea.setVisibility(View.INVISIBLE); <--
String inToken = mJsonManager.getToken();
mToken = inToken;
if (nager == null) {
getJsonData(GET, null, null, "wartaprofil", inToken);
} else {
getJsonData(GET, null, null, "wartarutin", inToken);
}
}
});
} catch (JSONException e) {
e.printStackTrace();
}
} else {
String jsonData = response.body().string();
switch(page){
case "wartaprofil":
try {
mJsonManager.setProfilStat(PrologueActivity.this, jsonData);
Log.v(TAG, jsonData);
runOnUiThread(new Runnable() {
@Override
public void run() {
--> openTitle(); <--
}
});
} catch (JSONException e) {
e.printStackTrace();
}
break;
case "wartarutin":
try {
mJsonManager.setWartaStat(PrologueActivity.this, jsonData);
Log.v(TAG, jsonData);
runOnUiThread(new Runnable() {
@Override
public void run() {
goToMainApp();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}
});
}
}
所以 json 数据被传递给 JsonManager 类并由该类保存到数据库。 我想从另一个活动中调用此方法,因此我不必在另一个活动中重复相同的方法..请分享您的想法..
【问题讨论】:
标签: java android performance android-studio okhttp3