Hi I'm linking a test android app to a MySQL database from an echo of JSON from a .php file.
I was able to populate an ArrayList with the whole data, but now i'd like to separate the data into variables and i can't really find out the way.
I'm using loopJ library to make the JSON communication
My MainActivity.java public class MainActivity extends AppCompatActivity {
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.lvLista);
getData();
}
public void getData()
{
AsyncHttpClient client = new AsyncHttpClient();
String url = "http://www.kukitosoft.com/android/api.php";
// RequestParams params = new RequestParams();
// params.put("campo", "valor a fitrar");
//Esto iria en el null del client.post
client.post(url, null, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
if (statusCode == 200)
{
loadList(getDataJSON(new String(responseBody)));
}
}
@Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
}
});
}
public void loadList(ArrayList<String> data)
{
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
list.setAdapter(adapter);
}
public ArrayList<String> getDataJSON(String response)
{
ArrayList<String> list = new ArrayList<String>();
try
{
JSONArray jsonArray = new JSONArray(response);
String texto;
for(int i=0; i<jsonArray.length(); i++)
{
texto = jsonArray.getJSONObject(i).getString("id") + " " +
jsonArray.getJSONObject(i).getString("nombre") + " " +
jsonArray.getJSONObject(i).getString("descripcion") + " " +
jsonArray.getJSONObject(i).getString("calificacion") + " ";
list.add(texto);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return list;
}
}