我如何将活动中的okhttp3类分隔到另一个类

时间:2018-10-19 00:30:59

标签: java android performance android-studio okhttp3

我在活动中创建了okhttp3方法并运行良好,但随后我想从另一个活动中调用该okhttp3方法,因此我认为我需要通过创建单独的类来将此okhttp3分开,因此我需要帮助如何创建单独的okhttp3类。 / p>

这是我在活动中创建的方法

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类,并由此类保存到数据库。 我想从另一个活动中调用此方法,所以我不必在另一个活动中重复相同的方法..请分享您的想法..

1 个答案:

答案 0 :(得分:1)

  1. 您可以创建一个BaseActivity,然后所有活动对其进行扩展,
  2. 将您的方法移至BaseActivity,
  3. 您可以在任何扩展BaseActivity的Activity中调用它。

以下是您的BaseActivity

public class BaseActivity extends Activity{ 
    //here is your method body
    public String methodA(Runnable run){
        //handle your result in run
        runOnUiThread(run);
        return "hello-world";
    }
}

关注您的ActivityA

public class ActivityA extends BaseActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //invoke methodA
        String myData = methodA(new Runnable(){
        @Override
        public void run() {
            //handle json data
        }
    });
    }
}