Contoh kode untuk mengambil data jsonArray dari android ke PHP dengan menggunakan library volley

Berikut contoh untuk mengambil data jsonArray dari android ke PHP dengan menggunakan library volley, silahkan di pelajari dan dikembangkan. Awalnya sulit kesananya mudah.

File MainActivity.java

package com.example.test;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;


public class MainActivity extends Activity {
	String server_url = "http://192.168.1.101/api/user.php";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.penjualantambah);
		
		makeJsonArrayRequest();		
	}

	private void makeJsonArrayRequest() {
		 
		final RequestQueue requestQueue = Volley.newRequestQueue(PenjualanTambah.this);
 
        JsonArrayRequest req = new JsonArrayRequest(server_url,
		    new Response.Listener<JSONArray>() {
	        @Override
	        public void onResponse(JSONArray response) {
	    		
	            try {
	                // Parsing json array response
	            	// loop through each json object                            
                   for (int i = 0; i < response.length(); i++) {

                        JSONObject person = (JSONObject) response.get(i);

                        String username = person.getString("username");
                        Toast.makeText(PenjualanTambah.this, username, Toast.LENGTH_LONG).show();
                    
                    }                            
	 
                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(),
                            "Error: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {                        
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();                        
            }
        });
		 
        // Adding request to request queue        
        requestQueue.add(req);
    }
 
}

Josh smoga bermanfaat gan.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s