Skip to content

Instantly share code, notes, and snippets.

@fanjavaid
Created April 13, 2014 13:46
Show Gist options
  • Save fanjavaid/10584858 to your computer and use it in GitHub Desktop.
Save fanjavaid/10584858 to your computer and use it in GitHub Desktop.
Retrieve data from URL and return as JSON and then processed by this Activity in AsyncTask.
package com.fanjavaid.valore.app;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONParser;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class MataKuliahActivity extends ListActivity implements SearchView.OnQueryTextListener {
private ListView lv;
private Context context;
private static String url_all_data = "http://fanjavaid.com/droidunindra/proc_select_matkul.php";
private ProgressDialog pDialog;
// Membuat JSON Parser Object
JSONParser parser = new JSONParser();
ArrayList<HashMap<String, String>> matkulList;
private static final String TAG_SUCCESS = "success";
private static final String TAG_MATKULS = "list_matkul";
private static final String TAG_MID = "id";
private static final String TAG_NAME = "matkul";
JSONArray matkuls = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mata_kuliah);
matkulList = new ArrayList<HashMap<String, String>>();
new LoadMatkuls().execute();
lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String mid = ((TextView) view.findViewById(R.id.id)).getText().toString();
// Starting new intent
// Intent intent = new Intent(getApplicationContext(), )
context = getApplicationContext();
Toast.makeText(context,mid,Toast.LENGTH_SHORT).show();
}
});
lv.setTextFilterEnabled(true);
ActionBar ab = getActionBar();
ab.setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.mata_kuliah, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(this);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText)) {
lv.clearTextFilter();
} else {
lv.setFilterText(newText.toString());
}
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
class LoadMatkuls extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MataKuliahActivity.this);
pDialog.setMessage("Loading Data");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
Intent intent = getIntent();
String semesterId = intent.getStringExtra("semesterId");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("semester", semesterId));
JSONObject json = parser.makeHttpRequest(url_all_data, "GET", params);
Log.d("All Datas : ", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// Data ditemukan
// Dapatkan array dari product
matkuls = json.getJSONArray(TAG_MATKULS);
// looping data
for (int i = 0; i < matkuls.length(); i++) {
JSONObject c = matkuls.getJSONObject(i);
// Simpan tiap data ke variabel
String id = c.getString(TAG_MID);
String name = c.getString(TAG_NAME);
// Buat HashMap
HashMap<String, String> map = new HashMap<String, String>();
// Tambahkan setiap child dari node ke HashMap
map.put(TAG_MID, id);
map.put(TAG_NAME, name);
matkulList.add(map);
}
} else {
// Data tidak ditemukan
//context = getApplicationContext();
Toast.makeText(MataKuliahActivity.this, "Data gagal diload", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
// Update UI dari Background Thread
runOnUiThread(new Runnable() {
@Override
public void run() {
// Updating parsed JSON Data ke ListView
ListAdapter adapter = new SimpleAdapter(
MataKuliahActivity.this, matkulList,
R.layout.list_matkul, new String [] {TAG_MID, TAG_NAME},
new int [] {R.id.id, R.id.matkul});
setListAdapter(adapter);
}
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment