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 org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.NameValuePair;
- import org.apache.http.ParseException;
- 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.Toast;
- public class CurrentSeasonDrivers extends Activity {
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.currentseason_drivers);
- new HttpTask().execute();
- }
- JSONArray jArray;
- String result = "";
- String drivername = "";
- String drivesfor = "";
- InputStream is = null;
- StringBuilder sb = null;
- private Activity CurrentSeasonDrivers = this;
- // STUFF GOES HERE
- public class HttpTask extends AsyncTask<String, String, String> {
- @Override
- protected String doInBackground(String... params) {
- // HTTP POST REQUEST
- try{
- ArrayList<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.this.runOnUiThread(new Runnable() {
- public void run() {
- Toast.makeText(CurrentSeasonDrivers, "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.this.runOnUiThread(new Runnable() {
- public void run() {
- Toast.makeText(CurrentSeasonDrivers, "Could not convert data to string", Toast.LENGTH_LONG).show(); } });
- }
- // PARSE STRING
- try {
- jArray = new JSONArray(result);
- JSONObject json_data = null;
- System.out.println("Length " + jArray.length());
- Log.d("DB", "Length " + jArray.length());
- for (int i = 0; i < jArray.length(); i++) {
- json_data = jArray.getJSONObject(i);
- drivername = json_data.getString("Driver_full_name");
- drivesfor = json_data.getString("Drives_for");
- System.out.println(drivername + "&" + drivesfor);
- }
- } catch (JSONException e1) {
- Log.d("DB", "Error somewhere");
- CurrentSeasonDrivers.this.runOnUiThread(new Runnable() {
- public void run() {
- Toast.makeText(CurrentSeasonDrivers, "Could not convert data to string", Toast.LENGTH_LONG).show(); } });
- }
- return null;
- }}
- }
Advertisement
Add Comment
Please, Sign In to add comment