Webview on android shows HTML

Asked

Viewed 451 times

1

I’m developing an android app, which I use a webservice to pick up news from a website and play pro app. These news comes in HTML and I take a webview and I set the HTML to it, and it works perfectly in the API’s level >= 19. I’m using the wbConteudo.loadDataWithBaseURL("file:///android_asset/",noticia.getConteudo(),"text/html; charset=utf-8", "utf-8",null); to set data in the webview, because there are news that are fixed and I decided to leave the photos in the "Asset" folder of android and then load them along with the HTML content.

But when I use a level < 19 api, the webview shows me the pure HTML code for the user instead of the formatted one ("test"), I’d like to know what’s going on, because I need it to work on the level API below 19.

Ex:

inserir a descrição da imagem aqui

I did a test here, and using the wbConteudo.loadData(noticia.getConteudo(),"text/html; charset=utf-8", "utf-8"); and it worked the HTML content, but the internal photos that are in the folder "Asset" do not work because I am not using the wbConteudo.loadDataWithBaseURL("file:///android_asset/",noticia.getConteudo(),"text/html; charset=utf-8", "utf-8",null);

So I’d like to know what’s going on and why it’s happening and a possible solution.

Tela Code:

public class NoticiaFragment extends Fragment {
private Noticia noticia;
public static Integer aba;

public NoticiaFragment(){

}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle bundle = getArguments();
    noticia = new Noticia(bundle.getString("titulo"), bundle.getString("conteudo"));
    NoticiaFragment.aba = bundle.getInt("ABA");
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_noticia,null);
    TextView txTitulo = (TextView) view.findViewById(R.id.txTitulo);
    WebView wbConteudo = (WebView) view.findViewById(R.id.wbConteudo);
    txTitulo.setText(noticia.getTitulo());
    wbConteudo.loadDataWithBaseURL("file:///android_asset/",noticia.getConteudo(),"text/html; charset=utf-8", "utf-8",null);//Isso funciona perfeitamente API >= 19
    //wbConteudo.loadData(noticia.getConteudo(),"text/html; charset=utf-8", "utf-8");//Isso funciona em API < 19
    wbConteudo.setBackgroundColor(Color.TRANSPARENT);

    return view;
}

public static int getAba() {
    return aba;
}

public static void setAba(int aba) {
    NoticiaFragment.aba = aba;
}

}

Layout Code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_noticia"
>

<TextView
    android:text="Titulo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/txTitulo"
    android:layout_marginTop="16dp"
    android:layout_marginBottom="16dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:textAppearance="@style/tituloNoticia"
    android:gravity="center"/>

<WebView
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:id="@+id/wbConteudo" /></LinearLayout>

Grid:

apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion "24.0.2"
defaultConfig {
    applicationId "br.com.patrick.fetiep"
    minSdkVersion 16
    targetSdkVersion 25
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.0.1'
compile 'com.android.support:support-v4:25.0.1'
compile 'com.android.support:design:25.0.1'

compile 'org.jetbrains:annotations-java5:15.0'

compile files('libs/activation.jar')
compile files('libs/additionnal.jar')
compile files('libs/mail.jar')
compile files('libs/gson-2.2.4-sources.jar')
compile files('libs/org.jbundle.util.osgi.wrapped.org.apache.http.client-4.1.2.jar')
compile files('libs/picasso-2.5.2.jar')

}

  • Man, it’s gonna be really hard for someone to come in here and tell you exactly where your mistake is... All I can tell you is to be careful with the version you’re using to develop. Developer.Android’s own official website already "Recommends using API 19+ version). What I can tell you is that only a low percentage of people use versions prior to this. You can check here the versions that are being used most at the moment. And if you have doubts about which methods still continue in this version change, you can see

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.