Created
December 20, 2018 10:55
-
-
Save SirLordPouya/cdd196cbeba2ffc4f687bd9ec48994e6 to your computer and use it in GitHub Desktop.
Using android HttpURLConnection to connect to internet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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