I am new to LoopJ Android Asynchronous-Http-Client.I figured out how to GET the data from the database with my PHP web service.
My ServiceHandler :
public class ServiceHandler {
private static String BASE_URL="http:/my/url/";
private static String GET_CAT= BASE_URL+"get_cat.php";
private static AsyncHttpClient client= new AsyncHttpClient();
public static void getAllCat(RequestParams params,AsyncHttpResponseHandler asyncHttpResponseHandler
){
client.get(GET_CAT,params,asyncHttpResponseHandler);};}
My AddBookActivity: In this class i use getAllCat to get categories from database but now how can i populate my spinner ?
public class AddBookActivity extends AppCompatActivity {
ServiceHandler client = new ServiceHandler();
private Spinner spCat;
@Override
protected void onResume() {
super.onResume();
getAllCat();
}
private void getAllCat(){
client.getAllCat(null, new JsonHttpResponseHandler(){
@Override
public void onSuccess(int statusCode,cz.msebera.android.httpclient.Header[] headers, JSONArray response) {
try {
for (int i=0; i<response.length();i++){
Categories cat = new Categories();
JSONObject obj =response.getJSONObject(i);
cat.setName(obj.getString("name"));
}
}catch (Exception e){
e.printStackTrace();
}}} ); }
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_book);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
spCat=(Spinner)findViewById(R.id.spinnercatname);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
}