I want to have an application use networking operation on its main thread without it throwing a NetworkOnMainThreadException. I don't want to use async codes to get around this. I heard that you can get around the NetworkOnMainThreadException without using async codes by using this code instead, from this answer link: "How to fix 'android.os.NetworkOnMainThreadException'?"
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
The answer stated that you can use this code in the class and it would ignore the NetworkOnMainThreadException and the code would execute without any problems, but when I tried it in my class I got the "identifier expected" error. What am I doing wrong?
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.widget.EditText;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public class myClass {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
public void connect(View v) throws IOException {
Document doc = Jsoup.connect("http://www.google.com//").get();
}
}
}