Alec 3 gadi atpakaļ
vecāks
revīzija
84d440e1a4

+ 1 - 0
.idea/gradle.xml

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
+  <component name="GradleMigrationSettings" migrationVersion="1" />
   <component name="GradleSettings">
     <option name="linkedExternalProjectsSettings">
       <GradleProjectSettings>

+ 0 - 64
app/src/main/java/com/example/mytest/API.java

@@ -1,64 +0,0 @@
-package com.example.mytest;
-
-import org.json.JSONArray;
-import org.json.JSONObject;
-
-import java.io.DataInputStream;
-import java.net.HttpURLConnection;
-import java.net.URL;
-
-public class API extends Thread {
-
-    // адрес запроса
-    public String url;
-    // параметры, передаваемые с запросом
-    public String param;
-
-    public Interface context;
-
-    @Override
-    public void run() {
-
-        try {
-            URL _url = new URL(url);
-
-            HttpURLConnection connection =
-                    (HttpURLConnection) _url.openConnection();
-
-            connection.setRequestMethod("POST");
-            connection.setRequestProperty("Content-Type", "application/json");
-            connection.setDoOutput(true);
-            connection.setDoInput(true);
-
-            connection.getOutputStream().write(param.getBytes());
-            connection.getOutputStream().flush();
-
-            connection.connect();
-
-            DataInputStream inputStream =
-                    new DataInputStream(connection.getInputStream());
-            String response = inputStream.readLine();
-
-            JSONObject json = new JSONObject(response);
-
-            if (json.has("Error")) {
-                if (!json.getString("Error").equals("null")) {
-                    System.out.println(json.getString("Error"));
-                    return;
-                }
-
-                if (json.has("News")) {
-                    if (!json.getString("News").equals("null")) {
-                        JSONArray news = json.getJSONArray("News");
-
-                        context.Main(news);
-                    }
-                }
-            }
-
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-
-    }
-}

+ 81 - 0
app/src/main/java/com/example/mytest/Fetch.java

@@ -0,0 +1,81 @@
+package com.example.mytest;
+
+import android.provider.ContactsContract;
+import android.widget.Button;
+
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+import java.io.BufferedReader;
+import java.io.DataInputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+public class Fetch extends Thread {
+
+    // адрес запроса
+    private String url;
+    // параметры, передаваемые с запросом
+    private String param;
+    // ссылка на MainActivity
+    private Share context;
+
+    public Fetch(String url, String param, Share context) {
+        this.url = url;
+        this.param = param;
+        this.context = context;
+    }
+
+    @Override
+    public void run() {
+
+        try {
+            URL _url = new URL(url);
+
+            HttpURLConnection connection = (HttpURLConnection) _url.openConnection();
+
+            connection.setRequestMethod("POST");
+            connection.setRequestProperty("Content-Type", "application/json");
+            connection.setDoOutput(true);
+            connection.setDoInput(true);
+
+            OutputStream outputStream = connection.getOutputStream();
+
+            outputStream.write(param.getBytes());
+            outputStream.flush();
+
+            connection.connect();
+
+            if (connection.getResponseCode() == 200) {
+
+                InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream());
+                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
+                String response = bufferedReader.readLine();
+
+                JSONObject json = new JSONObject(response);
+
+                if (json.has("Error")) {
+                    if (!json.getString("Error").equals("null")) {
+                        System.out.println(json.getString("Error"));
+                        return;
+                    }
+
+                    if (json.has("News")) {
+                        if (!json.getString("News").equals("null")) {
+                            JSONArray news = json.getJSONArray("News");
+
+                            context.ParseNews(news);
+                        }
+                    }
+                }
+
+            }
+
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+    }
+}

+ 0 - 7
app/src/main/java/com/example/mytest/Interface.java

@@ -1,7 +0,0 @@
-package com.example.mytest;
-
-import org.json.JSONArray;
-
-public interface Interface {
-    void Main(JSONArray array);
-}

+ 33 - 13
app/src/main/java/com/example/mytest/MainActivity.java

@@ -10,33 +10,55 @@ import android.widget.TextView;
 
 import org.json.JSONArray;
 import org.json.JSONObject;
+import org.w3c.dom.Text;
 
-public class MainActivity extends Activity implements Interface {
+public class MainActivity extends Activity implements Share {
 
-    private Interface context;
+    private Share context;
 
     @Override
-    public void Main(final JSONArray array) {
+    public void ParseNews(final JSONArray array) {
 
         runOnUiThread(new Runnable() {
             @Override
             public void run() {
-
-                LinearLayout news = findViewById(R.id.newContainer);
+                LinearLayout news = findViewById(R.id.newsContainer);
 
                 try {
                     for (int i = 0; i < array.length(); i++) {
 
                         JSONObject o = array.getJSONObject(i);
 
+                        LinearLayout.LayoutParams newsItemLayout = new LinearLayout.LayoutParams(
+                                LinearLayout.LayoutParams.MATCH_PARENT,
+                                LinearLayout.LayoutParams.WRAP_CONTENT
+                        );
+                        newsItemLayout.bottomMargin = 20;
+
+                        LinearLayout newsItem = new LinearLayout(getApplicationContext());
+                        newsItem.setOrientation(LinearLayout.VERTICAL);
+                        newsItem.setBackgroundColor(Color.WHITE);
+                        newsItem.setPadding(20, 20, 20, 20);
+                        newsItem.setLayoutParams(newsItemLayout);
+
                         TextView caption = new TextView(getApplicationContext());
 
                         if (o.has("Caption")) {
                             caption.setText(o.getString("Caption"));
-                            caption.setPadding(0, 20, 0, 20);
-                            caption.setTextColor(Color.RED);
-                            news.addView(caption);
+                            caption.setTextSize(20);
+                            caption.setPadding(0, 0, 0, 40);
+                            newsItem.addView(caption);
+                        }
+
+                        TextView preview = new TextView(getApplicationContext());
+
+                        if (o.has("Preview")) {
+                            preview.setText(o.getString("Preview"));
+                            preview.setLines(6);
+                            newsItem.addView(preview);
                         }
+
+                        news.addView(newsItem);
                     }
                 } catch (Exception e) {
                     e.printStackTrace();
@@ -54,14 +76,12 @@ public class MainActivity extends Activity implements Interface {
         context = this;
 
         Button button = findViewById(R.id.button);
+
         button.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
-                API api = new API();
-                api.url = "https:/ktk-45.ru/news";
-                api.param = "{\"Page\": 0,\"Limit\": 10}";
-                api.context = context;
-                api.start();
+                Fetch fetch = new Fetch("https://ktk-45.ru/news", "{\"Page\": 0,\"Limit\": 10}", context);
+                fetch.start();
             }
         });
 

+ 7 - 0
app/src/main/java/com/example/mytest/Share.java

@@ -0,0 +1,7 @@
+package com.example.mytest;
+
+import org.json.JSONArray;
+
+public interface Share {
+    void ParseNews(JSONArray array);
+}

+ 13 - 8
app/src/main/res/layout/activity_main.xml

@@ -17,16 +17,21 @@
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toTopOf="parent" />
 
-    <LinearLayout
-        android:id="@+id/newContainer"
-        android:layout_width="409dp"
-        android:layout_height="536dp"
-        android:layout_marginStart="1dp"
-        android:layout_marginEnd="1dp"
-        android:orientation="vertical"
+    <ScrollView
+        android:layout_width="match_parent"
+        android:layout_height="0dp"
+        android:layout_marginTop="24dp"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
-        app:layout_constraintTop_toBottomOf="@+id/button"></LinearLayout>
+        app:layout_constraintTop_toBottomOf="@+id/button">
+
+        <LinearLayout
+            android:id="@+id/newsContainer"
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:background="#DFDFDF"
+            android:orientation="vertical" />
+    </ScrollView>
 
 </androidx.constraintlayout.widget.ConstraintLayout>