getString and DialogListener doesn't work after class switch

允我心安 提交于 2020-01-06 07:29:24

问题


I want to start an activity and give out a Toast message after I parsed a JSON response, well as you can't do that while a Dialog is open, I am using a DialogListener, which actually works fine, but not when it gets called in the parseJSON method.

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);
    }
I already tried the runOnUIThread, but it doesn't help. Thank you very much in advance

回答1:


Solved it by putting all the stuff in an activity an not in the DialogFragment.

Thank you to everyone :)



来源:https://stackoverflow.com/questions/51662401/getstring-and-dialoglistener-doesnt-work-after-class-switch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!