Okhttp3使应用程序在请求加载之前关闭活动时崩溃 - java

一切正常。但是,我正在加载问题片段,然后立即单击返回并退出应用程序,而无需完成请求。它使应用程序崩溃。我尝试环顾四周,但找不到可以帮助我防止崩溃的修复程序。我附上logcatQuestions类以供参考。

日志猫

12-11 15:37:44.898 16772-16835/com.aftertutor.app E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
                                                                    Process: com.aftertutor.app, PID: 16772
                                                                    java.lang.NullPointerException
                                                                        at com.aftertutor.app.Question$1.onResponse(Question.java:112)
                                                                        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
                                                                        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
                                                                        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                        at java.lang.Thread.run(Thread.java:841)

问题类别

package com.aftertutor.app;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.aftertutor.app.model.API;
import com.dpizarro.autolabel.library.AutoLabelUI;
import com.dpizarro.autolabel.library.AutoLabelUISettings;
import com.github.thunder413.datetimeutils.DateTimeStyle;
import com.github.thunder413.datetimeutils.DateTimeUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Date;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class Question extends Fragment {

    public String id;
    public TextView title;
    public TextView desc;
    public TextView date;
    public TextView edit;
    public TextView report;
    public TextView username;
    public AutoLabelUI tags;
    public CardView cardView;
    public ProgressBar progressBar;
    public TextView votes;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_question, null);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        // Equivalent to on Create
        super.onViewCreated(view, savedInstanceState);

        getActivity().setTitle("Loading...");

        title = view.findViewById(R.id.ques_title);
        desc = view.findViewById(R.id.ques_desc);
        date = view.findViewById(R.id.ques_time);
        edit = view.findViewById(R.id.ques_edit);
        report = view.findViewById(R.id.ques_report);
        username = view.findViewById(R.id.ques_username);
        tags = view.findViewById(R.id.ques_labels);
        cardView = view.findViewById(R.id.ques_card);
        progressBar = view.findViewById(R.id.ques_progress);
        votes = view.findViewById(R.id.ques_votes);

        Bundle arg = getArguments();
        if (arg != null && arg.containsKey("id")) {
            id = arg.getString("id");
        }

        loadQuestion(id);

    }

    public void loadQuestion(String qid) {
        final API api = new API();
        OkHttpClient client = new OkHttpClient();

        RequestBody body = new FormBody.Builder()
                .add("qid", qid)
                .build();

        Request request = api.call("get_question", body, getContext(), getActivity());

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                call.cancel();
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        MaterialDialog dialog = api.prepareDialog(getContext(), "An Error Occoured", "An Error Occoured. Please make sure you are connected to the internet and try again. If the issue still persists please contact support.");
                        dialog.show();
                    }
                });
            }

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

                final String responseText = response.body().string();
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {

                            JSONObject jsonObject = new JSONObject(responseText);

                            if (jsonObject.get("status").equals("ok"))
                            {
                                JSONObject payload = jsonObject.getJSONObject("payload");
                                JSONObject questionItem = payload.getJSONObject("question");

                                title.setText(questionItem.get("title").toString());
                                getActivity().setTitle(questionItem.get("title").toString());
                                desc.setText(questionItem.get("description").toString());
                                date.setText(DateTimeUtils.getTimeAgo(getContext(), new Date(questionItem.getInt("postedOn") * 1000L), DateTimeStyle.AGO_FULL_STRING));
                                votes.setText(questionItem.getInt("votes") + "");
                                username.setText("@" + questionItem.get("postedBy").toString());
                                String[] taggs = api.toStringArray(questionItem.getJSONArray("tags"));

                                edit.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        // Edit Page here
                                    }
                                });

                                report.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        MaterialDialog dialog = new MaterialDialog.Builder(getContext())
                                                .title("Report this question because...")
                                                .items(R.array.report_array)
                                                .itemsCallbackSingleChoice(0, new MaterialDialog.ListCallbackSingleChoice() {
                                                    @Override
                                                    public boolean onSelection(MaterialDialog dialog, View itemView, int which, CharSequence text) {
                                                        Toast.makeText(getContext(), "YOU SELECTED: " + which, Toast.LENGTH_SHORT).show();
                                                        return false;
                                                    }
                                                })
                                                .content("Reporting will let us know that something isn't wrong with this post. We will take action asap.")
                                                .positiveText("Report")
                                                .onPositive(new MaterialDialog.SingleButtonCallback() {
                                                    @Override
                                                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                                        Toast.makeText(getContext(), "Reported", Toast.LENGTH_SHORT).show();
                                                    }
                                                })
                                                .negativeText("Cancel")
                                                .build();
                                        dialog.show();
                                    }
                                });

                                AutoLabelUISettings autoLabelUISettings = new AutoLabelUISettings.Builder()
                                        .withMaxLabels(5)
                                        .withShowCross(false)
                                        .withLabelsClickables(false)
                                        .withBackgroundResource(R.drawable.rounded_corner)
                                        .withTextSize(R.dimen.custom_label_font_size)
                                        .withLabelPadding(R.dimen.custom_label_font_padding)
                                        .build();
                                tags.setSettings(autoLabelUISettings);
                                tags.clear();

                                for (String tag: taggs) {
                                    tags.addLabel(tag);
                                }
                                progressBar.setVisibility(View.GONE);
                                cardView.setVisibility(View.VISIBLE);

                            }
                            else if (jsonObject.get("status").equals("error"))
                            {
                                MaterialDialog dialog = api.prepareDialog(getContext(), jsonObject.getJSONObject("dialog").get("title").toString(), jsonObject.getJSONObject("dialog").get("message").toString());
                                dialog.show();
                            }
                            else
                            {
                                MaterialDialog dialog = api.prepareDialog(getContext(), "An Error Occurred", "An Error Occurred. Please make sure you are connected to the internet and try again. If the issue still persists please contact support.");
                                dialog.show();
                            }

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

            }
        });

    }

}

参考方案

您必须在onPause()中取消通话,因此将通话作为字段:

private Call call;

然后将loadQuestion()方法更改为:

call = client.newCall(request);
call.enqueue(new Callback() {

并取消请求:

@Override
public void onPause() {
    super.onPause();
    if (call != null){
        call.cancel();
    }
}

Android Firebase:将数据保存到数据库 - java

我正在尝试从Firebase保存和检索数据,但是在获取正确的语法时遇到了一些麻烦。用户通过电子邮件和密码的身份验证方法进行注册,并使用相同的详细信息登录。我有两个编辑文本字段,要求输入名称和语句。在两个编辑文本框下面有一个按钮,用于将数据保存到数据库。private void saveQuote(){ String name = author.getText…

当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java

我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…

java.net.URI.create异常 - java

java.net.URI.create("http://adserver.adtech.de/adlink|3.0") 抛出java.net.URISyntaxException: Illegal character in path at index 32: http://adserver.adtech.de/adlink|3.0 虽然n…

如何在Wiremock中为JUNIT匹配精确的json - java

我正在使用Wiremock在Spring启动应用程序中模拟Junit的REST服务。我的问题是,我无法匹配多个匹配模式。 Junit.javaStringValuePattern pattern = WireMock.matching(".*"); givenThat(post(urlEqualTo("/softwares�…

AppCompat不支持当前主题 - java

我的应用在Android N上运行正常,但在Android M上的setContentView(R.layout.activity_main)崩溃: Caused by: java.lang.IllegalArgumentException: AppCompat does not support the current theme features: { w…