Advertisement
Guest User

Untitled

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