Advertisement
Guest User

Untitled

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