Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RandomUserSearchActivity extends Activity {
- private String jsonResult;
- private String url = "http://testapp.wc.lt/_TEST.php";
- private ListView listView;
- Button btnRandom;
- EditText inputAgeFrom;
- EditText inputAgeTo;
- private ProgressDialog pDialog;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.random_user_find);
- listView = (ListView) findViewById(R.id.listView1);
- inputAgeFrom = (EditText) findViewById(R.id.inputAgeFrom);
- inputAgeTo = (EditText) findViewById(R.id.inputAgeTo);
- btnRandom = (Button)findViewById(R.id.btnRandomUser);
- btnRandom.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- accessWebService();
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- //getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- // Async Task to access the web
- private class JsonReadTask extends AsyncTask<String, Void, String> {
- protected void onPreExecute() {
- super.onPreExecute();
- pDialog = new ProgressDialog(RandomUserSearchActivity.this);
- pDialog.setMessage("Loading product details. Please wait...");
- pDialog.setIndeterminate(false);
- pDialog.setCancelable(true);
- pDialog.show();
- }
- @Override
- protected String doInBackground(String... params) {
- HttpClient httpclient = new DefaultHttpClient();
- HttpPost httppost = new HttpPost(url);
- //List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
- // nameValuePairs.add(new BasicNameValuePair("ageSearchFrom", "5"));
- // nameValuePairs.add(new BasicNameValuePair("ageSearchTo", "25"));
- String ageFrom = inputAgeFrom.getText().toString();
- String ageTo = inputAgeFrom.getText().toString();
- try {
- List <NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
- nameValuePairs.add(new BasicNameValuePair("ageSearchFrom", ageFrom));
- //nameValuePairs.add(new BasicNameValuePair("ageSearchTo", ageTo));
- httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
- HttpResponse response = httpclient.execute(httppost);
- //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
- jsonResult = inputStreamToString(
- response.getEntity().getContent()).toString();
- }
- catch (ClientProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- private StringBuilder inputStreamToString(InputStream is) {
- String rLine = "";
- StringBuilder answer = new StringBuilder();
- BufferedReader rd = new BufferedReader(new InputStreamReader(is));
- try {
- while ((rLine = rd.readLine()) != null) {
- answer.append(rLine);
- }
- }
- catch (IOException e) {
- // e.printStackTrace();
- Toast.makeText(getApplicationContext(),
- "Error..." + e.toString(), Toast.LENGTH_LONG).show();
- }
- return answer;
- }
- @Override
- protected void onPostExecute(String result) {
- pDialog.dismiss();
- ListDrawer();
- }
- }// end async task
- public void accessWebService() {
- JsonReadTask task = new JsonReadTask();
- // passes values for the urls string array
- task.execute(new String[] { url });
- }
- // build hash set for list view
- public void ListDrawer() {
- List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();
- try {
- JSONObject jsonResponse = new JSONObject(jsonResult);
- JSONArray jsonMainNode = jsonResponse.optJSONArray("user");
- for (int i = 0; i < jsonMainNode.length(); i++) {
- JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
- String name = jsonChildNode.optString("name");
- String number = jsonChildNode.optString("age");
- String outPut = name + "-" + number;
- employeeList.add(createEmployee("employees", outPut));
- }
- } catch (JSONException e) {
- Toast.makeText(getApplicationContext(), "Error" + e.toString(),
- Toast.LENGTH_SHORT).show();
- }
- SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList,
- android.R.layout.simple_list_item_1,
- new String[] { "employees" }, new int[] { android.R.id.text1 });
- listView.setAdapter(simpleAdapter);
- }
- private HashMap<String, String> createEmployee(String name, String number) {
- HashMap<String, String> employeeNameNo = new HashMap<String, String>();
- employeeNameNo.put(name, number);
- return employeeNameNo;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement