Advertisement
Guest User

Untitled

a guest
Aug 31st, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.19 KB | None | 0 0
  1. package com.android.history;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8.  
  9. import org.apache.http.HttpEntity;
  10. import org.apache.http.HttpResponse;
  11. import org.apache.http.NameValuePair;
  12. import org.apache.http.client.HttpClient;
  13. import org.apache.http.client.entity.UrlEncodedFormEntity;
  14. import org.apache.http.client.methods.HttpPost;
  15. import org.apache.http.impl.client.DefaultHttpClient;
  16. import org.json.JSONArray;
  17. import org.json.JSONException;
  18. import org.json.JSONObject;
  19.  
  20. import android.app.Activity;
  21. import android.os.AsyncTask;
  22. import android.os.Bundle;
  23. import android.util.Log;
  24. import android.widget.ArrayAdapter;
  25. import android.widget.ListView;
  26. import android.widget.Toast;
  27.  
  28. public class CurrentSeasonDrivers extends Activity {
  29.    
  30.     ListView currentList;
  31.     String output;
  32.     JSONArray jArray;
  33.     String result = "";
  34.     String drivername;
  35.     String drivesfor;
  36.     InputStream is = null;
  37.     StringBuilder sb = null;
  38.    
  39.     public void onCreate(Bundle savedInstanceState) {
  40.         super.onCreate(savedInstanceState);
  41.         setContentView(R.layout.currentseason_drivers);
  42.         new HttpTask().execute();
  43.     }
  44.    
  45.     private Activity CurrentSeasonDrivers = this;
  46.     List<String> nameData = new ArrayList<String>();
  47.     // ASYNCTASK
  48.     public class HttpTask extends AsyncTask<String, String, String> {
  49.  
  50.         @Override
  51.         protected String doInBackground(String... params) {
  52.            
  53.             // HTTP POST REQUEST
  54.             try{
  55.                 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  56.                 HttpClient httpclient = new DefaultHttpClient();
  57.                
  58.                 HttpPost httppost = new HttpPost("http://192.168.0.13/testdatabase.php");
  59.                 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  60.  
  61.                 HttpResponse response = httpclient.execute(httppost);
  62.                 HttpEntity entity = response.getEntity();
  63.  
  64.                 is = entity.getContent();
  65.                
  66.             }catch(Exception e){
  67.                 CurrentSeasonDrivers.this.runOnUiThread(new Runnable() {
  68.                     public void run() {
  69.                         Toast.makeText(CurrentSeasonDrivers, "Could not connect to server", Toast.LENGTH_LONG).show(); } });
  70.             }
  71.  
  72.             //CONVERT DATA TO STRING
  73.             try {
  74.                 BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
  75.                 sb = new StringBuilder();
  76.                 sb.append(reader.readLine() + "\n");
  77.  
  78.                 String line;
  79.                 while ((line = reader.readLine()) != null) {
  80.                     sb.append(line);
  81.                 }
  82.                 is.close();
  83.                 result = sb.toString();
  84.                 Log.i("json string", result);
  85.                
  86.             }catch (Exception e) {
  87.                 CurrentSeasonDrivers.this.runOnUiThread(new Runnable() {
  88.                     public void run() {
  89.                         Toast.makeText(CurrentSeasonDrivers, "Could not convert data to string", Toast.LENGTH_LONG).show(); } });
  90.             }
  91.            
  92.             // PARSE STRING TO JSON
  93.             try {
  94.                 jArray = new JSONArray(result);
  95.                 JSONObject json_data = null;
  96.                 currentList = (ListView) findViewById(R.id.list);
  97.                
  98.                
  99.                
  100.                 for (int i = 0; i < jArray.length(); i++) {
  101.                        
  102.                     json_data = jArray.getJSONObject(i);
  103.                     nameData.add(drivername = json_data.getString("Driver_full_name"));
  104.                     nameData.add(drivesfor = json_data.getString("Drives_for"));  
  105.                    
  106.                 }
  107.                
  108.             } catch (JSONException e1) {
  109.                 Log.d("DB", "Error somewhere");
  110.                 CurrentSeasonDrivers.this.runOnUiThread(new Runnable() {
  111.                     public void run() {
  112.                         Toast.makeText(CurrentSeasonDrivers, "Could not parse data so shut up", Toast.LENGTH_LONG).show(); } });
  113.             }
  114.        
  115.             return output; 
  116.             }
  117.        
  118.         protected void onPostExecute(String output){
  119.            
  120.             currentList.setAdapter(new ArrayAdapter<String>(CurrentSeasonDrivers.this, android.R.layout.simple_list_item_2, nameData));
  121.             Toast.makeText(CurrentSeasonDrivers, "DONE!", Toast.LENGTH_LONG).show();
  122.            
  123.         }  
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement