Skip to content

Instantly share code, notes, and snippets.

@pratamawijaya
Created October 11, 2013 17:12
Show Gist options
  • Save pratamawijaya/6938487 to your computer and use it in GitHub Desktop.
Save pratamawijaya/6938487 to your computer and use it in GitHub Desktop.
Contoh penggunaan library volley
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.volleyexample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.volleyexample.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.example.volleyexample;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends Activity
{
// url webservice
String url = "http://api.pratamawijaya.com/jkt48/member.php";
// queue untuk request
private RequestQueue queue;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
queue = Volley.newRequestQueue(this);
JsonObjectRequest jsonReq = new JsonObjectRequest(Request.Method.GET,
url, null, new Response.Listener<JSONObject>() {
public void onResponse(JSONObject response)
{
try
{
JSONArray array = response
.getJSONArray("jkt48");
JSONObject obj = array.getJSONObject(1);
Toast.makeText(MainActivity.this,
"" + obj.getString("nama"),
Toast.LENGTH_SHORT).show();
} catch (JSONException e)
{
}
};
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error)
{
}
});
queue.add(jsonReq);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment