有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java改造,TimeoutException onFailure

我正在使用改装认证,我有一个timeoutexception。我看到了很多问题,但我不能解决这个问题

这是密码

public class FragmentRegistration extends Fragment {
    View mainView;

    EditText username, email, password, name;
    Button button;

    ApiClient pentairAPIClient = ApiClient.getInstance();

    SupportopObj supportopObj = new SupportopObj();
    SupportopObjActivate supportopObjActivate = new SupportopObjActivate();

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mainView = inflater.inflate
                (R.layout.registration, container, false);

        username = mainView.findViewById(R.id.username);
        email = mainView.findViewById(R.id.email);
        password = mainView.findViewById(R.id.password);
        name = mainView.findViewById(R.id.name);
        button = mainView.findViewById(R.id.register);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //String s = name.getText().toString();
                //String split[] = s.split(" ");

                supportopObj.setFirstName("Tester");
                supportopObj.setLastName("Testryan");
                supportopObj.setUsername(username.getText().toString());
                supportopObj.setEmail(email.getText().toString());
                supportopObj.setPassword(password.getText().toString());

                supportopObjActivate.setUsername(supportopObj.getUsername());
                supportopObjActivate.setEmail(supportopObj.getEmail());
                supportopObjActivate.setPassword(supportopObj.getPassword());
                supportopObjActivate.setType("generic");
                updateApp();               
            }
        });

        return mainView;
    }


    public void updateApp() {

        Call<ResponseBody> call = pentairAPIClient.registration(supportopObj);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful()) {
                    activationCall();

                } else {
                    Toast.makeText(getContext(), "Something went wrong",
                            Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(getContext(), "Error...", Toast.LENGTH_SHORT).show();
            }
        });
    }


    public void activationCall() {
        Call<ResponseBody> callActive = pentairAPIClient.activation(supportopObjActivate);
        callActive.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                if (response.isSuccessful()) {

                    try {
                        String data = response.body().string();
                        JSONObject obj = new JSONObject(data);
                        String client_id = obj.getString("client_id");
                        String client_secret = obj.getString("client_secret");

                        tokenCall(client_id, client_secret);

                    } catch (JSONException | IOException e) {
                        e.printStackTrace();
                    }

                } else {
                    Toast.makeText(getContext(), "error", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(getContext(), "Error in activation",
                        Toast.LENGTH_SHORT).show();
            }
        });
    }


    public void tokenCall(String client_id, String client_secret) {
        Call<ResponseBody> token = pentairAPIClient.get_token("password", client_id, client_secret,
                supportopObj.getEmail(), supportopObj.getPassword());

        token.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.isSuccessful()) {
                    Toast.makeText(getContext(), String.valueOf(response.body()), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getContext(), "Something wrong.....", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(getContext(), "You're on failure", Toast.LENGTH_SHORT).show();
            }
        });
    }

我在改装过程中没有错误,我正在进行调试,我看到了所有过程,因此我成功地获得了客户机id和密码,然后在上一个onfailure()中获得了timeoutexception

 @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) 
{
    Toast.makeText(getContext(), "You're onfailure",Toast.LENGTH_SHORT).show();
} 

注意代码,这是最后一行。如何修复它?应用程序没有崩溃,但在调试中,他会像这样删除timeoutexception
t={SocketTimeoutException@830038722120}java.net.SocketTimeoutException: timeout

在Okhttp客户端中

readTimeout(10, TimeUnit.SECONDS).connectTimeout(10, TimeUnit.SECONDS).writeTimeout(10, TimeUnit.SECONDS); 

全部都是10,我试着设置为100,没有帮助。帮助我。谢谢


共 (1) 个答案

  1. # 1 楼答案

    @Hayk Mkrtchyan

    就在你的

    onFailure(Call<ResponseBody> call, Throwable t) {
         System.out.println(t.getMessage());  //you will get "timeout" as a string in case of actual timeout
    }
    

    应用程序控制通常会进入超时状态

    1)Api响应缓慢

    2)检查网络连接

    另外,10秒的连接超时非常低,请尝试将其设置为至少60秒