类切换后,getString和DialogListener不起作用

时间:2018-08-02 21:29:03

标签: android json dialog toast android-toast

我想在解析JSON响应后开始活动并发出Toast消息,而且当Dialog打开时您无法执行此操作,我使用的是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,但这没有帮助。 提前非常感谢您

1 个答案:

答案 0 :(得分:0)

通过将所有内容放入活动而不是在DialogFragment中解决了该问题。

谢谢大家:)