OkHttp客户端始终调用OnFailure

时间:2018-07-19 08:40:43

标签: android okhttp

我在使用OkHttp库时遇到问题,我试图找到原因所在,但未发现任何问题。我创建OkHttp客户端,然后通过API端点调用服务器。我总是在OnFailure界面中获得callback方法。 请参见下面的代码:

public class RegisterDialogFrag extends DialogFragment implements View.OnClickListener {

    @Override
    public void onClick(View view) {
        try {
            switch (view.getId()){
                case R.id.button_register_ok: signUp();
                    break;
                case R.id.button_register_cancel: dismiss();
                    break;
            }
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }

    }

    private void signUp() throws Exception{
        String authorName = etUsername.getText().toString();
        String authorPw = etPassword.getText().toString();
        String authorMail = etMailID.getText().toString();
        author = new Author(0, authorName, authorMail, authorPw);
        if(Util.isAppOnline(contextReference.get())){
            progressBar.setVisibility(View.VISIBLE);

            okHttpClient = new OkHttpClient.Builder()
                    .connectTimeout(5000, TimeUnit.MICROSECONDS)
                    .readTimeout(5000, TimeUnit.MICROSECONDS)
                    .build();

            Request request = new Request.Builder()
                    // http://todolistmobileapp-env.ap-south-1.elasticbeanstalk.com/webapi/authors
                    .url(ToDoRestAPIs.baseRemoteHostUrl + ToDoRestAPIs.registerAuthor)
                    .addHeader("Content-Type", "application/json")
                    .post(RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(author)))
                    .build();

            okHttpClient.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            progressBar.setVisibility(View.INVISIBLE);
                        }
                    });
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {

                    try {
                        author = new Gson().fromJson(response.body().string(), Author.class);
                    } catch (JsonSyntaxException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            progressBar.setVisibility(View.INVISIBLE);
                            if(registerListener != null)
                                registerListener.onRegisterSuccess(author);
                            Toast.makeText(contextReference.get(), "register success!", Toast.LENGTH_SHORT).show();
                            dismiss();
                        }
                    });

                }
            });
        }// end if
    }
}

大家知道吗?谢谢

0 个答案:

没有答案