Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.65 KB | None | 0 0
  1. public class RandomUserSearchActivity extends Activity {
  2.     private String jsonResult;
  3.     private String url = "http://testapp.wc.lt/_TEST.php";
  4.     private ListView listView;
  5.  
  6.     Button btnRandom;
  7.    
  8.     EditText inputAgeFrom;
  9.     EditText inputAgeTo;
  10.    
  11.     private ProgressDialog pDialog;
  12.  
  13.     @Override
  14.     protected void onCreate(Bundle savedInstanceState) {
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.random_user_find);
  17.         listView = (ListView) findViewById(R.id.listView1);
  18.         inputAgeFrom = (EditText) findViewById(R.id.inputAgeFrom);
  19.         inputAgeTo = (EditText) findViewById(R.id.inputAgeTo);
  20.         btnRandom = (Button)findViewById(R.id.btnRandomUser);
  21.         btnRandom.setOnClickListener(new View.OnClickListener() {
  22.             @Override
  23.             public void onClick(View view) {
  24.                 accessWebService();
  25.             }
  26.         });
  27.  
  28.  }
  29.  
  30.  @Override
  31.  public boolean onCreateOptionsMenu(Menu menu) {
  32.      // Inflate the menu; this adds items to the action bar if it is present.
  33.      //getMenuInflater().inflate(R.menu.main, menu);
  34.      return true;
  35.  }
  36.  
  37.  // Async Task to access the web
  38.  private class JsonReadTask extends AsyncTask<String, Void, String> {
  39.      
  40.      protected void onPreExecute() {
  41.          super.onPreExecute();
  42.          pDialog = new ProgressDialog(RandomUserSearchActivity.this);
  43.          pDialog.setMessage("Loading product details. Please wait...");
  44.          pDialog.setIndeterminate(false);
  45.          pDialog.setCancelable(true);
  46.          pDialog.show();
  47.      }
  48.      @Override
  49.      protected String doInBackground(String... params) {
  50.          HttpClient httpclient = new DefaultHttpClient();
  51.          HttpPost httppost = new HttpPost(url);
  52.          
  53.          //List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
  54.            // nameValuePairs.add(new BasicNameValuePair("ageSearchFrom", "5"));
  55.            // nameValuePairs.add(new BasicNameValuePair("ageSearchTo", "25"));
  56.          
  57.          String ageFrom = inputAgeFrom.getText().toString();
  58.          String ageTo = inputAgeFrom.getText().toString();
  59.          
  60.          try {
  61.              
  62.              List <NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
  63.              nameValuePairs.add(new BasicNameValuePair("ageSearchFrom", ageFrom));
  64.              //nameValuePairs.add(new BasicNameValuePair("ageSearchTo", ageTo));
  65.              httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  
  66.              
  67.              HttpResponse response = httpclient.execute(httppost);
  68.  
  69.              //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  70.              
  71.  
  72.              jsonResult = inputStreamToString(
  73.                      response.getEntity().getContent()).toString();  
  74.          }
  75.  
  76.          catch (ClientProtocolException e) {
  77.              e.printStackTrace();
  78.          } catch (IOException e) {
  79.              e.printStackTrace();
  80.          }
  81.          return null;
  82.          
  83.      }
  84.  
  85.      private StringBuilder inputStreamToString(InputStream is) {
  86.          String rLine = "";
  87.          StringBuilder answer = new StringBuilder();
  88.          BufferedReader rd = new BufferedReader(new InputStreamReader(is));
  89.  
  90.          try {
  91.              while ((rLine = rd.readLine()) != null) {
  92.                  answer.append(rLine);
  93.              }
  94.          }
  95.  
  96.          catch (IOException e) {
  97.              // e.printStackTrace();
  98.              Toast.makeText(getApplicationContext(),
  99.                      "Error..." + e.toString(), Toast.LENGTH_LONG).show();
  100.          }
  101.          return answer;
  102.      }
  103.  
  104.   @Override
  105.   protected void onPostExecute(String result) {
  106.       pDialog.dismiss();
  107.       ListDrawer();
  108.   }
  109.  }// end async task
  110.  
  111.  
  112.  public void accessWebService() {
  113.   JsonReadTask task = new JsonReadTask();
  114.   // passes values for the urls string array
  115.   task.execute(new String[] { url });
  116.  }
  117.  
  118.  // build hash set for list view
  119.  public void ListDrawer() {
  120.   List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();
  121.  
  122.  
  123.  
  124.   try {
  125.    JSONObject jsonResponse = new JSONObject(jsonResult);
  126.    JSONArray jsonMainNode = jsonResponse.optJSONArray("user");
  127.  
  128.    for (int i = 0; i < jsonMainNode.length(); i++) {
  129.     JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
  130.     String name = jsonChildNode.optString("name");
  131.     String number = jsonChildNode.optString("age");
  132.     String outPut = name + "-" + number;
  133.     employeeList.add(createEmployee("employees", outPut));
  134.    }
  135.   } catch (JSONException e) {
  136.    Toast.makeText(getApplicationContext(), "Error" + e.toString(),
  137.      Toast.LENGTH_SHORT).show();
  138.   }
  139.  
  140.   SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList,
  141.     android.R.layout.simple_list_item_1,
  142.     new String[] { "employees" }, new int[] { android.R.id.text1 });
  143.   listView.setAdapter(simpleAdapter);
  144.  }
  145.  
  146.  private HashMap<String, String> createEmployee(String name, String number) {
  147.   HashMap<String, String> employeeNameNo = new HashMap<String, String>();
  148.   employeeNameNo.put(name, number);
  149.   return employeeNameNo;
  150.  }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement