Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public StringRequest(int method,
- String url,
- com.android.volley.Response.Listener<String> listener,
- com.android.volley.Response.ErrorListener errorListener)
- StringRequest postRequest = new StringRequest(Request.Method.POST, url,
- new Response.Listener<String>() {
- @Override
- public void onResponse(String response) { **...** }
- package ru.startandroid.bitrix_site_integration;
- import android.net.Uri;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.TextView;
- import com.android.volley.Request;
- import com.android.volley.RequestQueue;
- import com.android.volley.Response;
- import com.android.volley.VolleyError;
- import com.android.volley.toolbox.StringRequest;
- import com.android.volley.toolbox.Volley;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- public class NewsActivity extends MainActivity implements Response.Listener<String> {
- TextView textView;
- LinearLayout ll;
- static final String[] arr = {"ID", "NAME", "PREVIEW_TEXT", "PREVIEW_PICTURE", "PROPERTY_*"};
- private class News {
- String id;
- String name;
- String previewText;
- String previewPicture;
- private News(String id, String name, String previewText, String previewPicture) {
- this.id = id;
- this.name = name;
- this.previewText = previewText;
- this.previewPicture = previewPicture;
- }
- }
- @Override
- public void onResponse(String response) {
- ll.removeAllViews();
- try {
- JSONArray jsonObject = new JSONArray(response);
- if (jsonObject.length() != 0) {
- ArrayList<News> news = new ArrayList<News>();
- for (int i = 0; i < jsonObject.length(); i++) {
- JSONObject c = jsonObject.getJSONObject(i);
- News newInfo = new News(
- c.getString("ID"),
- c.getString("NAME"),
- c.getString("PREVIEW_TEXT"),
- c.getString("PREVIEW_PICTURE")
- );
- news.add(newInfo);
- }
- this.setNews(news);
- } else {
- TextView textEmpty = new TextView(this);
- textEmpty.setText("Нет новостей");
- ll.addView(textEmpty);
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.news);
- textView = (TextView) findViewById(R.id.textView);
- ll = (LinearLayout) findViewById(R.id.parent);
- RequestQueue queue = Volley.newRequestQueue(this);
- String url = super.siteUrl + super.bitrixApiFolder + "getList.php";
- StringRequest postRequest = new StringRequest(Request.Method.POST, url,
- this,
- new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- // error
- Log.d("Error.Response", error.getMessage());
- }
- }
- ) {
- @Override
- protected Map<String, String> getParams() {
- Map<String, String> params = new HashMap<String, String>();
- params.put("IBLOCK_CODE", "furniture_news_s1");
- JSONObject jsonObject = new JSONObject();
- for (Integer i = 0; i < arr.length; i++) {
- try {
- jsonObject.put((i.toString()), arr[i]);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- params.put("arSelect", jsonObject.toString());
- return params;
- }
- };
- queue.add(postRequest);
- }
- protected void setNews(ArrayList<News> news) {
- for (News newInfo : news) {
- LinearLayout lln = new LinearLayout(this);
- lln.setOrientation(LinearLayout.HORIZONTAL);
- if (newInfo.previewPicture != null) {
- ImageView image = new ImageView(this);
- image.setImageURI(Uri.parse(newInfo.previewPicture));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement