Guest User

Untitled

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