Please help, as the code below that should send a GET request does not work.
Behaviour: after tapping the button it writes "Running", then uses some traffic, then after some time hangs and crashes.
My goal is to send HTTP GET and handle the response as a string. I have tried a lot of options but they failed.
I'll greatly appreciate any working way of HTTP requests that NOT run in the UI (main) thread. Requests in the main thread are no more allowed and throw a networkonmainthreadexception.
Thanks in advance!
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
MainActivity.java (I'll delete waste imports later):
package com.aohdhgdsb.helloworld;
import android.app.DownloadManager;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ImageView;
import android.widget.Toast;
import android.os.Handler;
import android.widget.TextView.OnEditorActionListener;
import android.view.inputmethod.EditorInfo;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import android.os.AsyncTask;
import java.io.InputStream;
import java.net.URLConnection;
public class MainActivity extends AppCompatActivity {
public EditText Edit1;
public EditText Edit2;
public TextView Text1;
public ImageView Img1;
public Button But1;
@Override
protected void onCreate(Bundle savedInstanceState) {
showToast("Hi. I have started!\r\nv6");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void showToast(String toast){
try {
Toast toast2 = Toast.makeText(getApplicationContext(),
"" + toast,
Toast.LENGTH_SHORT);
//toast2.setGravity(Gravity.TOP, 0, 0);
toast2.show();
} catch (Throwable e) {
showToast("Error while showing the toast!");
/*
Toast toast3 = Toast.makeText(getApplicationContext(),
"TOAST ERR: " + e,
Toast.LENGTH_LONG);
toast3.show();
*/
}
}
public String readStream(InputStream is) {
try {
ByteArrayOutputStream bo = new ByteArrayOutputStream();
int i = is.read();
while(i != -1) {
bo.write(i);
i = is.read();
}
return bo.toString();
} catch (IOException e) {
return "";
}
}
public void Go(View v) {
handleResponse("Running...");
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
//******************************************
try{
URL url = new URL("http://example.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try{
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
handleResponse(readStream(in));
} catch (Throwable e) {showToast("ERROR7: " + e);}
finally {
urlConnection.disconnect();
}
} catch (Throwable e) {
showToast("ERROR6: " + e);
}
//****************************************
}
});
thread.start();
}
public void handleResponse(String str){
Edit2 = (EditText) findViewById(R.id.editText2);
Edit2.setText(str);
}
}
Part of activity_main.xml:
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go!"
android:id="@+id/button3"
android:layout_gravity="center_horizontal"
android:onClick="Go" />
<EditText
android:layout_width="match_parent"
android:layout_height="256dp"
android:id="@+id/editText2"
android:layout_gravity="center_horizontal"
android:layout_weight="8.25"
android:text="Nothing" />