Guest User

CurrentSeasonDrivers.java

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