MainActivity.java 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package com.example.mytest;
  2. import android.app.Activity;
  3. import android.graphics.Color;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.LinearLayout;
  8. import android.widget.TextView;
  9. import org.json.JSONArray;
  10. import org.json.JSONObject;
  11. public class MainActivity extends Activity implements Interface {
  12. private Interface context;
  13. @Override
  14. public void Main(final JSONArray array) {
  15. runOnUiThread(new Runnable() {
  16. @Override
  17. public void run() {
  18. LinearLayout news = findViewById(R.id.newContainer);
  19. try {
  20. for (int i = 0; i < array.length(); i++) {
  21. JSONObject o = array.getJSONObject(i);
  22. TextView caption = new TextView(getApplicationContext());
  23. if (o.has("Caption")) {
  24. caption.setText(o.getString("Caption"));
  25. caption.setPadding(0, 20, 0, 20);
  26. caption.setTextColor(Color.RED);
  27. news.addView(caption);
  28. }
  29. }
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. });
  35. }
  36. @Override
  37. protected void onCreate(Bundle savedInstanceState) {
  38. super.onCreate(savedInstanceState);
  39. setContentView(R.layout.activity_main);
  40. context = this;
  41. Button button = findViewById(R.id.button);
  42. button.setOnClickListener(new View.OnClickListener() {
  43. @Override
  44. public void onClick(View view) {
  45. API api = new API();
  46. api.url = "https:/ktk-45.ru/news";
  47. api.param = "{\"Page\": 0,\"Limit\": 10}";
  48. api.context = context;
  49. api.start();
  50. }
  51. });
  52. }
  53. }