1

New to programing in java and on Android device. My issue is that I can't seem to get the postcontent variable to set for the web view to use it. I can confirm that the postcontent is correct when I log the value after getting the data. But the web view never sees the that postcontent was set with new data.

package org.appname.app.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.util.Log;
import org.appname.app.calendar.*;
import java.io.IOException;
import okhttp3.*;



public class CalendarFragment extends android.support.v4.app.Fragment {

    public static CalendarFragment newInstance() {
        return new CalendarFragment();
    }
    public static final String TAG = CalendarFragment.class.getSimpleName();
    private static final String calendarJSON = "https://www.example.com/.json";
    String postcontent = "";
    private String postTest = "<p><h3>CACHED</h3></p><p><strong>Saturday, May 5</strong></p>";



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(org.appname.app.R.layout.fragment_giving, container, false);

        final WebView mWebView = view.findViewById(org.appname.app.R.id.giving_webview);


            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                    .url(calendarJSON)
                    .build();

            Call call = client.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

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

                    try {
                        String jsonData = response.body().string();
                        if (response.isSuccessful()) {
                            CalendarHelper data = Converter.fromJsonString(jsonData);
                            ObjectElement[] objects = data.getObjects();
                            postcontent = objects[0].getPostBody();
                            Log.v(TAG, postcontent.toString());
                        } else {
                            //alertUserAboutError();
                        }
                    } catch (IOException e) {
                        Log.v(TAG, "Exception caught : ", e);
                    }
                }
            });

        if (postcontent.isEmpty()) {
            Log.v(TAG, "empty");
            mWebView.loadDataWithBaseURL(null, postTest, "text/html", "utf-8", null);

        } else {
            mWebView.loadDataWithBaseURL(null, postcontent, "text/html", "utf-8", null);

            Log.v(TAG, "not empty");
        };

        return view;
    }

}

2 Answers 2

1

call.enqueue() is an asynchronous call, which the call is not a blocking call. But it provide callback, such as onResponse, onFailure when the call finishes.

To update UI state when the asynchronous call succeed, simply do it in onResponse callback. mHandler.post() is used here to ensure UI updates happen only in UI thread.

Similarly, to update UI when the asynchronous call fails, use similar technique in onFailure callback.

// to be used to update UI in UI thread
private Handler mHandler;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(org.stmarcus.stmarcusmke.R.layout.fragment_giving, container, false);

    final WebView mWebView = view.findViewById(org.stmarcus.stmarcusmke.R.id.giving_webview);

    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
            .url(calendarJSON)
            .build();

    // instantiate mHandler
    mHandler = new Handler(Looper.getMainLooper());

    Call call = client.newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {

        }

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

            try {
                String jsonData = response.body().string();
                if (response.isSuccessful()) {
                    CalendarHelper data = Converter.fromJsonString(jsonData);
                    ObjectElement[] objects = data.getObjects();
                    postcontent = objects[0].getPostBody();
                    Log.v(TAG, postcontent.toString());

                    // update UI in UI Thread
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            if (postcontent.isEmpty()) {
                                Log.v(TAG, "empty");
                                mWebView.loadDataWithBaseURL(null, postTest, "text/html", "utf-8", null);

                            } else {
                                mWebView.loadDataWithBaseURL(null, postcontent, "text/html", "utf-8", null);

                                Log.v(TAG, "not empty");
                            };
                        }
                    });

                } else {
                    //alertUserAboutError();
                }
            } catch (IOException e) {
                Log.v(TAG, "Exception caught : ", e);
            }
        }
    });
Sign up to request clarification or add additional context in comments.

Comments

0

Call loadData() method of WebView to load simple html data

mWebView.loadData(postcontent, "text/html", "UTF-8");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.