<?php
// DEVOLVE TODAS AS PESSOAS COM TODA A INFO
$app->get('/api/pessoas', function () {
require_once('db/dbconnect.php');
$myArray = array();
foreach($db->pessoa()
->order("id")
as $row){
array_push($myArray, array('nome' => $row["nome"], 'idade' =>
$row["idade"]));
$result = ['status' => true, 'data' => $myArray];
}
if(isset($result)){
echo json_encode($result, JSON_UNESCAPED_UNICODE);
}else{
echo "Não existem resultados";
}
});
this is the web service I am using and it's giving me the correct answer of every person in the db with all attributes
{"status":true,"data":[{"nome":"Paulo","idade":"23"},{"nome":"Manuela","idade":"23"},{"nome":"Paulo2","idade":"25"},{"nome":"Maranhao Faria","idade":"888"}]}
Now, I want to receive it in Java and fill a list with it:
public class ListInfo extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_info);
String url = "paulomaranhao.000webhostapp.com/myslim/api/pessoas";
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try{
JSONArray array = response.getJSONArray("data");
List<JSONObject> items = new ArrayList<>();
for(int i = 0; i < array.length(); i++){
items.add(array.getJSONObject(i));
}
ArrayAdapter<JSONObject> adapter = new ArrayAdapter<>(ListInfo.this, android.R.layout.simple_list_item_1, items);
((ListView) findViewById(R.id.list)).setAdapter(adapter);
}catch(JSONException e){}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(ListInfo.this, getResources().getString(R.string.errorShowing), Toast.LENGTH_SHORT). show();
return;
}
});
// Access the RequestQueue through your singleton class.
MySingleton.getInstance(this).addToRequestQueue(jsObjRequest);
}
When I debug it does not even enter the onResponse. After that I want to fill the list, if possible customized, or just with the name of the person.
Thank you so much!!!
Solved my problem! I was adding it to the queue, MySingleton, but it was not initialized in the begining. It was just missing this RequestQueue declaration :
private RequestQueue queue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rest_post);
queue = MySingleton.getInstance(this.getApplicationContext()).
getRequestQueue();
}
The internet permission is added and working as the other points because I already have two different activities working properly, one that inserts a person on the db and one that gets the info of a person by id, and all of that works. Also the URL in the browser is correct
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.