Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. public StringRequest(int method,
  2. String url,
  3. com.android.volley.Response.Listener<String> listener,
  4. com.android.volley.Response.ErrorListener errorListener)
  5.  
  6. StringRequest postRequest = new StringRequest(Request.Method.POST, url,
  7. new Response.Listener<String>() {
  8. @Override
  9. public void onResponse(String response) { **...** }
  10.  
  11. package ru.startandroid.bitrix_site_integration;
  12.  
  13. import android.net.Uri;
  14. import android.os.Bundle;
  15. import android.util.Log;
  16. import android.widget.ImageView;
  17. import android.widget.LinearLayout;
  18. import android.widget.TextView;
  19.  
  20. import com.android.volley.Request;
  21. import com.android.volley.RequestQueue;
  22. import com.android.volley.Response;
  23. import com.android.volley.VolleyError;
  24. import com.android.volley.toolbox.StringRequest;
  25. import com.android.volley.toolbox.Volley;
  26.  
  27. import org.json.JSONArray;
  28. import org.json.JSONException;
  29. import org.json.JSONObject;
  30.  
  31. import java.util.ArrayList;
  32. import java.util.HashMap;
  33. import java.util.Map;
  34.  
  35. public class NewsActivity extends MainActivity implements Response.Listener<String> {
  36.  
  37. TextView textView;
  38. LinearLayout ll;
  39. static final String[] arr = {"ID", "NAME", "PREVIEW_TEXT", "PREVIEW_PICTURE", "PROPERTY_*"};
  40.  
  41. private class News {
  42. String id;
  43. String name;
  44. String previewText;
  45. String previewPicture;
  46.  
  47. private News(String id, String name, String previewText, String previewPicture) {
  48. this.id = id;
  49. this.name = name;
  50. this.previewText = previewText;
  51. this.previewPicture = previewPicture;
  52. }
  53. }
  54.  
  55. @Override
  56. public void onResponse(String response) {
  57. ll.removeAllViews();
  58. try {
  59. JSONArray jsonObject = new JSONArray(response);
  60. if (jsonObject.length() != 0) {
  61. ArrayList<News> news = new ArrayList<News>();
  62. for (int i = 0; i < jsonObject.length(); i++) {
  63. JSONObject c = jsonObject.getJSONObject(i);
  64. News newInfo = new News(
  65. c.getString("ID"),
  66. c.getString("NAME"),
  67. c.getString("PREVIEW_TEXT"),
  68. c.getString("PREVIEW_PICTURE")
  69. );
  70. news.add(newInfo);
  71. }
  72. this.setNews(news);
  73. } else {
  74. TextView textEmpty = new TextView(this);
  75. textEmpty.setText("Нет новостей");
  76. ll.addView(textEmpty);
  77. }
  78.  
  79. } catch (JSONException e) {
  80. e.printStackTrace();
  81. }
  82. }
  83.  
  84. @Override
  85. protected void onCreate(Bundle savedInstanceState) {
  86. super.onCreate(savedInstanceState);
  87. setContentView(R.layout.news);
  88.  
  89. textView = (TextView) findViewById(R.id.textView);
  90. ll = (LinearLayout) findViewById(R.id.parent);
  91.  
  92. RequestQueue queue = Volley.newRequestQueue(this);
  93. String url = super.siteUrl + super.bitrixApiFolder + "getList.php";
  94. StringRequest postRequest = new StringRequest(Request.Method.POST, url,
  95. this,
  96. new Response.ErrorListener() {
  97. @Override
  98. public void onErrorResponse(VolleyError error) {
  99. // error
  100. Log.d("Error.Response", error.getMessage());
  101. }
  102. }
  103. ) {
  104. @Override
  105. protected Map<String, String> getParams() {
  106. Map<String, String> params = new HashMap<String, String>();
  107.  
  108. params.put("IBLOCK_CODE", "furniture_news_s1");
  109.  
  110. JSONObject jsonObject = new JSONObject();
  111. for (Integer i = 0; i < arr.length; i++) {
  112. try {
  113. jsonObject.put((i.toString()), arr[i]);
  114. } catch (JSONException e) {
  115. e.printStackTrace();
  116. }
  117. }
  118.  
  119. params.put("arSelect", jsonObject.toString());
  120.  
  121. return params;
  122. }
  123. };
  124. queue.add(postRequest);
  125.  
  126. }
  127.  
  128. protected void setNews(ArrayList<News> news) {
  129. for (News newInfo : news) {
  130. LinearLayout lln = new LinearLayout(this);
  131. lln.setOrientation(LinearLayout.HORIZONTAL);
  132. if (newInfo.previewPicture != null) {
  133. ImageView image = new ImageView(this);
  134. image.setImageURI(Uri.parse(newInfo.previewPicture));
  135. }
  136. }
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement