@dedrick
To send a JSON object from CodeIgniter to Android, you can follow these steps:
1 2 3 4 5 6 7 8 |
class Api extends CI_Controller { public function get_data() { $data = array('name' => 'John Doe', 'age' => 30, 'city' => 'New York'); header('Content-Type: application/json'); echo json_encode($data); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
String url = "http://yourdomain.com/api/get_data"; JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { try { String name = response.getString("name"); int age = response.getInt("age"); String city = response.getString("city"); // Do something with the data } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { error.printStackTrace(); } }); RequestQueue requestQueue = Volley.newRequestQueue(this); requestQueue.add(jsonObjectRequest); |
1
|
<uses-permission android:name="android.permission.INTERNET" /> |
By following these steps, you will be able to send a JSON object from CodeIgniter to Android and handle the response in your Android application.