Created
October 11, 2013 17:12
-
-
Save pratamawijaya/6938487 to your computer and use it in GitHub Desktop.
Contoh penggunaan library volley
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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