Skip to content

Instantly share code, notes, and snippets.

@SirLordPouya
Created December 20, 2018 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SirLordPouya/cdd196cbeba2ffc4f687bd9ec48994e6 to your computer and use it in GitHub Desktop.
Save SirLordPouya/cdd196cbeba2ffc4f687bd9ec48994e6 to your computer and use it in GitHub Desktop.
Using android HttpURLConnection to connect to internet
package ir.mftvanak.mftfridays;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class TestWebActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test_web);
new Thread(new Runnable() {
@Override
public void run() {
try {
URL obj = new URL("https://google.com");
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", "Mozilla/5.0");
int responseCode = con.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
System.out.println(response.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment