com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
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.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
ArrayList<String> mContacts;
RecyclerView mRecyclerView;
RecyclerView.LayoutManager mLayoutManger;
RecyclerView.Adapter mAdapter;
String cpTitleS;
TextView cpTitle;
Map<String, String> items;
JSONArray arr = new JSONArray();
private List listItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.setProperty("file.encoding", "UTF-8");
//
listItems = new ArrayList<>();
items = new HashMap<String, String>();
// String URL = "https://test.manana.life/v1/search.getList";
cpTitle = (TextView) findViewById(R.id.cpTitle);
Api();
}
public void Api(){
RequestQueue requestQueue = Volley.newRequestQueue(this);
String URL = "https://test.manana.life/v1/search.getList?";
JsonObjectRequest objectRequest = new JsonObjectRequest(
Request.Method.GET,
URL,
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject object = response.getJSONObject("response");
arr = object.getJSONArray("items");
System.out.println(arr.length());
for (int i=0;i<arr.length();i++) {
JSONObject o = (JSONObject)arr.get(i);
listItems.add(o);
System.out.println(listItems.size());
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println(error);
Log.e("response error", error.toString());
}
}
);
System.out.println("вне try"+arr.length());
requestQueue.add(objectRequest);
mRecyclerView = findViewById(R.id.rcView);
mContacts = new ArrayList<>();
mContacts.add(cpTitleS);
mContacts.add("Упаковщик");
for (int i = 0;i<100; i++)
{
mContacts.add("Упаковщик #" + i);
}
mRecyclerView.setHasFixedSize(true);
mLayoutManger = new LinearLayoutManager(this);
mAdapter = new MainADapter(mContacts);
mRecyclerView.setLayoutManager(mLayoutManger);
mRecyclerView.setAdapter(mAdapter);
}
}
Это асинхронный запрос, его колбэк может вызваться позже чем print "вне try"
Обсуждают сегодня