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;
}
}