class 切换后 getString 和 DialogListener 不起作用

getString and DialogListener doesn't work after class switch

我想在解析 JSON 响应后启动 activity 并发出 Toast 消息,而且当对话框打开时你不能这样做,我正在使用DialogListener,它实际上工作正常,但在 parseJSON 方法中被调用时就不行了。

public class Pop_Forgot_PW extends AppCompatDialogFragment{
     
      ......

    sendResetMail();


private void sendResetMail()
    {
        final String url = "someURL";

        new Json(new Json.Callback() {
            @Override
            public void run(String result) {
                parseJSON(result);
            }
        }).checkJsonFile(url, getContext());
    }



 //Now in an non-activity class

  public class Json {

    public void checkJsonFile(final String url, final Context context) {

        new Thread(new Runnable() {
            public void run() {
                String result;
                String line;

                try {

                    URL obj = new URL(url);
                    HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
                    conn.setReadTimeout(5000);
                    conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
                    conn.addRequestProperty("User-Agent", "Mozilla");
                    conn.addRequestProperty("Referer", "google.com");


                    boolean redirect = false;

                    // normally, 3xx is redirect
                    int status = conn.getResponseCode();
                    if (status != HttpURLConnection.HTTP_OK) {
                        if (status == HttpURLConnection.HTTP_MOVED_TEMP
                                || status == HttpURLConnection.HTTP_MOVED_PERM
                                || status == HttpURLConnection.HTTP_SEE_OTHER)
                            redirect = true;
                    }

                    if (redirect) {

                        // get redirect url from "location" header field
                        String newUrl = conn.getHeaderField("Location");

                        // get the cookie if need, for login
                        String cookies = conn.getHeaderField("Set-Cookie");

                        // open the new connnection again
                        conn = (HttpURLConnection) new URL(newUrl).openConnection();
                        conn.setRequestProperty("Cookie", cookies);
                        conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
                        conn.addRequestProperty("User-Agent", "Mozilla");
                        conn.addRequestProperty("Referer", "google.com");


                    }

                    BufferedReader in = new BufferedReader(
                            new InputStreamReader(conn.getInputStream()));

                    StringBuilder sb = new StringBuilder();

                    while ((line = in.readLine()) != null) {
                        sb.append(line);

                    }
                    in.close();

                    result = sb.toString();

                    callback.run(result);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}

//Back in the Pop_Up
public void parseJSON(String JSON) {
        try {
            JSONObject jsonObject = new JSONObject(JSON);
            error = jsonObject.getInt("error_code");
switch (error) {
            case 0: 

                toastText = getString(R.string.email_sent);
                break;
            case 1:
                toastText = getString(R.string.no_account);
                break;
        }
        listener.showToast(toastText);
        dismiss();

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

    public void setListener(DialogListener listener) {
        this.listener = listener;
    }

    public interface DialogListener
    {
        void showToast(String toastText);
    }
我已经尝试过 runOnUIThread,但没有用。 非常感谢您

通过将所有内容放在 activity 而不是 DialogFragment 中解决了这个问题。

谢谢大家:)