Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.android.history;
- import java.io.BufferedReader;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.StringTokenizer;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.entity.UrlEncodedFormEntity;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import android.app.Activity;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.TextView;
- import android.widget.Toast;
- public class CurrentSeasonDrivers_DriverName extends Activity {
- JSONArray jArray;
- String result = "";
- InputStream is = null;
- StringBuilder sb = null;
- String returnString, somethingelse;
- TextView drivername, drivesfor;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.currentseason_drivers);
- new HttpTask().execute();
- }
- private Activity CurrentSeasonDrivers_DriversName = this;
- // ASYNCTASK
- public class HttpTask extends AsyncTask<Void, Void, String[]> {
- @Override
- protected String[] doInBackground(Void... params) {
- // HTTP POST REQUEST
- try {
- List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
- HttpClient httpclient = new DefaultHttpClient();
- HttpPost httppost = new HttpPost("http://192.168.0.13/testdatabase.php");
- httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
- HttpResponse response = httpclient.execute(httppost);
- HttpEntity entity = response.getEntity();
- is = entity.getContent();
- } catch (Exception e) {
- CurrentSeasonDrivers_DriverName.this.runOnUiThread(new Runnable() {
- public void run() {
- Toast.makeText(CurrentSeasonDrivers_DriversName, "Could not connect to server", Toast.LENGTH_LONG)
- .show();
- }
- });
- }
- // CONVERT DATA TO STRING
- try {
- BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
- sb = new StringBuilder();
- sb.append(reader.readLine() + "\n");
- String line;
- while ((line = reader.readLine()) != null) {
- sb.append(line);
- }
- is.close();
- result = sb.toString();
- Log.i("json string", result);
- } catch (Exception e) {
- CurrentSeasonDrivers_DriverName.this.runOnUiThread(new Runnable() {
- public void run() {
- Toast.makeText(CurrentSeasonDrivers_DriversName, "Could not convert data to string",
- Toast.LENGTH_LONG).show();
- }
- });
- }
- //PARSING JSON DATA
- try {
- jArray = new JSONArray(result);
- for (int i = 0; i < jArray.length(); i++) {
- JSONObject json_data = jArray.getJSONObject(i);
- returnString = json_data.optString("Driver_full_name");
- somethingelse = json_data.optString("Drives_for");
- Log.i("work", returnString);
- Log.i("dontwork", somethingelse);
- }
- } catch (JSONException e1) {
- Log.d("DB", "Error somewhere");
- CurrentSeasonDrivers_DriverName.this.runOnUiThread(new Runnable() {
- public void run() {
- Toast.makeText(CurrentSeasonDrivers_DriversName, "Could not parse data so shut up",
- Toast.LENGTH_LONG).show();
- }
- });
- }
- return new String[] {returnString, somethingelse};
- }
- protected void onPostExecute(String[] strings) {
- Log.i("Google", returnString);
- Log.i("Google1", somethingelse);
- String returnString = strings[0];
- String somethingelse = strings[1];
- TextView drivername = (TextView) findViewById(R.id.DriverName);
- drivername.setText(returnString);
- TextView drivesfor = (TextView) findViewById(R.id.DrivesFor);
- drivesfor.setText(somethingelse);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement