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. :(