0

Its been like around a month now and I'm still struggling to send a simple request through android app. Tried several ways, used libraries yet failed. Hoped you people might see the mistake I couldn't. App crashes on start.

Here's the last the code I tried:

public class MainActivity extends Activity {

Handler handler2;
String result="";

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    MyThread mt= new MyThread();
    mt.start();
}


private class MyThread extends Thread{
    @Override
    public void run() {
        Toast.makeText(MainActivity.this,"thread created",Toast.LENGTH_LONG).show();
        URL url = null;
        try {
            url = new URL("http://www.android.com/");
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            try {                   
                InputStream in = new BufferedInputStream(urlConnection.getInputStream());
                result = readStream(in);
                Toast.makeText(MainActivity.this,"success",Toast.LENGTH_LONG).show();
            } finally {
                urlConnection.disconnect();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        handler2.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(MainActivity.this,result,Toast.LENGTH_LONG).show();
                }
            });

    }

And then there is logcat:

12-19 13:59:24.989 18412 18412 E   AndroidRuntime                               Process: com.system.alt, PID: 18412
12-19 13:59:24.989 18412 18412 E   AndroidRuntime                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.system.alt/com.system.alt.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
12-19 13:59:24.989 18412 18412 E   AndroidRuntime                               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)

I think there is problem in starting a new thread. Can anyone help? I'm badly stuck. :(

AB_498
  • 39
  • 9
  • What's on the line 35 of your MainActivity? That's where the error comes from according to the log. Also I don't think you can show a toast nor update any other ui elements directly from another thread, check this https://stackoverflow.com/questions/3134683/android-toast-in-a-thread One option is to use async task which gives you an easy way to access the ui thread https://developer.android.com/reference/android/os/AsyncTask – Pete Dec 19 '19 at 19:54
  • Actually I refreshed the build and found these logs. Updated. – AB_498 Dec 19 '19 at 20:21
  • Btw, commented out Toasts, still crashes. – AB_498 Dec 19 '19 at 20:25
  • Note that the error message has nothing to do with the HTTP request. In fact, Android hasn't executed your code far enough to even get to that yet. If you google the error message, you will find many links that show how to solve it, including the one I added to the top of your question. – Code-Apprentice Dec 19 '19 at 20:39

1 Answers1

0

Try to extend AppCompatActivity instead Activity

...
public class MainActivity extends AppCompatActivity{
...