Created
November 23, 2025 18:06
-
-
Save sunmeat/010de8988a3e342c7e064254a0aaa9f4 to your computer and use it in GitHub Desktop.
приклад чистого GET-запиту android
This file contains hidden or 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
| MainActivity.java: | |
| package site.sunmeat.helloworld; | |
| import android.os.Bundle; | |
| import android.widget.*; | |
| import androidx.appcompat.app.AppCompatActivity; | |
| import java.io.*; | |
| import java.net.*; | |
| import java.nio.charset.StandardCharsets; | |
| import java.util.concurrent.*; | |
| public class MainActivity extends AppCompatActivity { | |
| private EditText editUrl; | |
| private TextView textResult; | |
| private final ExecutorService executor = Executors.newSingleThreadExecutor(); | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.activity_main); | |
| editUrl = findViewById(R.id.editUrl); | |
| textResult = findViewById(R.id.textResult); | |
| Button btnLoad = findViewById(R.id.btnLoad); | |
| btnLoad.setOnClickListener(v -> { | |
| String host = editUrl.getText().toString().trim(); | |
| if (host.isEmpty()) { | |
| Toast.makeText(this, "Введіть адресу", Toast.LENGTH_SHORT).show(); | |
| return; | |
| } | |
| if (!host.contains(".")) { | |
| Toast.makeText(this, "Невірна адреса", Toast.LENGTH_SHORT).show(); | |
| return; | |
| } | |
| textResult.setText("Завантаження..."); | |
| executor.execute(() -> loadPage(host)); | |
| }); | |
| } | |
| private void loadPage(String host) { | |
| host = host.replace("http://", "").replace("https://", ""); | |
| if (host.contains("/")) { | |
| host = host.substring(0, host.indexOf("/")); | |
| } | |
| String request = "GET / HTTP/1.1\r\n" + | |
| "Host: " + host + "\r\n" + | |
| "Connection: close\r\n" + | |
| "User-Agent: AndroidHttpSocketDemo/1.0\r\n" + | |
| "\r\n"; | |
| Socket socket = null; | |
| try { | |
| InetAddress addr = InetAddress.getByName(host); | |
| socket = new Socket(addr, 80); | |
| socket.setSoTimeout(15000); | |
| // відправка запиту | |
| OutputStream out = socket.getOutputStream(); | |
| out.write(request.getBytes(StandardCharsets.US_ASCII)); | |
| out.flush(); | |
| // читання відповіді | |
| var reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8)); | |
| var response = new StringBuilder(); | |
| String line; | |
| while ((line = reader.readLine()) != null) | |
| response.append(line).append("\n"); | |
| String result = response.toString(); | |
| // оновлення UI в головному потоці | |
| runOnUiThread(() -> { | |
| textResult.setText(result); | |
| Toast.makeText(MainActivity.this, "Завантажено!", Toast.LENGTH_SHORT).show(); | |
| }); | |
| } catch (Exception e) { | |
| String error = "Помилка: " + e.getMessage(); | |
| runOnUiThread(() -> { | |
| textResult.setText(error); | |
| Toast.makeText(MainActivity.this, "Не вдалося підключитися", Toast.LENGTH_LONG).show(); | |
| }); | |
| } finally { | |
| if (socket != null) { | |
| try { | |
| socket.close(); | |
| } catch (Exception ignored) {} | |
| } | |
| } | |
| } | |
| @Override | |
| protected void onDestroy() { | |
| super.onDestroy(); | |
| // важливо закривати пул потоків при закритті актівіті | |
| executor.shutdownNow(); | |
| } | |
| } | |
| ================================================================================================================== | |
| activity_main.xml: | |
| <?xml version="1.0" encoding="utf-8"?> | |
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| android:orientation="vertical" | |
| android:padding="16dp"> | |
| <EditText | |
| android:id="@+id/editUrl" | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:hint="Введіть домен, наприклад google.com" | |
| android:text="google.com" /> | |
| <Button | |
| android:id="@+id/btnLoad" | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:text="Завантажити сторінку" /> | |
| <ScrollView | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| android:layout_marginTop="16dp"> | |
| <TextView | |
| android:id="@+id/textResult" | |
| android:layout_width="match_parent" | |
| android:layout_height="wrap_content" | |
| android:fontFamily="monospace" | |
| android:text="Тут буде HTML..." | |
| android:textSize="12sp" /> | |
| </ScrollView> | |
| </LinearLayout> | |
| ================================================================================================================== | |
| AndroidManifest.xml: | |
| ... | |
| <uses-permission android:name="android.permission.INTERNET" /> | |
| ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment