Android Open Source - BusTicketer Connection Runnable






From Project

Back to project page BusTicketer.

License

The source code is released under:

Copyright (c) 2013, Nelspike All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Red...

If you think the Android project BusTicketer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package bus.ticketer.connection;
/* w w  w. jav a 2 s  .c o  m*/
import java.io.*;
import java.net.*;
import java.util.*;

import org.apache.http.*;
import org.apache.http.message.*;
import org.json.*;

public class ConnectionRunnable implements Runnable {

  private String link, resultString, method;
  private ArrayList<NameValuePair> payload;
  private int readTimeout = 10000, connectionTimeout = 15000;
  private JSONObject resultObject;
  
  public ConnectionRunnable(String link, String method, ArrayList<NameValuePair> payload) {
    this.link = link;
    this.method = method;
    this.payload = payload;
  }
  
  @Override
  public void run() {
    connect();
  }
  
  private void connect() {
    HttpURLConnection con = null;
    String line = "";
    StringBuffer sb = new StringBuffer();
        try {
            
            URL url = new URL(link);
            con = (HttpURLConnection) url.openConnection();
            con.setReadTimeout(readTimeout);
            con.setConnectTimeout(connectionTimeout);
            con.setRequestMethod(method);
            con.setDoInput(true);

            if(payload != null) {
              con.setDoOutput(true);
                
              ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
              
              for(NameValuePair pair : payload)
                params.add(new BasicNameValuePair(pair.getName(), pair.getValue()));
              
              OutputStream os = con.getOutputStream();
              BufferedWriter writer = new BufferedWriter(
                      new OutputStreamWriter(os, "UTF-8"));
              writer.write(getQuery(params));
              writer.flush();
              writer.close();
              os.close();
            }
            
            // Start the connection
            con.connect();

            // Read results from the query
            BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8" ));
            
            while ((line = reader.readLine()) != null)
        sb.append(line);
            
            resultString = sb.toString();
            try {
        setResultObject(new JSONObject(resultString));
      } catch (JSONException e) {
        //Can't parse JSON
      }
            
            reader.close();
        }
        catch (IOException e) {
          e.printStackTrace();
          } 
        finally {
            if (con != null)
              con.disconnect();
         }
  }
  
  private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException  {
      StringBuilder result = new StringBuilder();
      boolean first = true;

      for (NameValuePair pair : params) {
          if (first)
              first = false;
          else
              result.append("&");

          result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
          result.append("=");
          result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
      }

      return result.toString();
  }

  public JSONObject getResultObject() {
    return resultObject;
  }

  public void setResultObject(JSONObject resultObject) {
    this.resultObject = resultObject;
  }

}




Java Source Code List

bus.ticketer.adapters.CentralPagerAdapter.java
bus.ticketer.adapters.DialogAdapter.java
bus.ticketer.adapters.SpinnerAdapter.java
bus.ticketer.connection.ConnectionRunnable.java
bus.ticketer.connection.ConnectionRunnable.java
bus.ticketer.connection.ConnectionRunnable.java
bus.ticketer.connection.ConnectionThread.java
bus.ticketer.connection.ConnectionThread.java
bus.ticketer.connection.ConnectionThread.java
bus.ticketer.filters.MonthInputFilter.java
bus.ticketer.fragments.BuyTicketsFragment.java
bus.ticketer.fragments.ShowTicketsFragment.java
bus.ticketer.inspector.BusInspector.java
bus.ticketer.inspector.MainActivity.java
bus.ticketer.inspector.ResultActivity.java
bus.ticketer.inspector.ScanActivity.java
bus.ticketer.listeners.BusTabListener.java
bus.ticketer.listeners.PurchaseListener.java
bus.ticketer.listeners.RadioGroupListener.java
bus.ticketer.listeners.SwipeListener.java
bus.ticketer.listeners.TicketPurchaseListener.java
bus.ticketer.listeners.ValidationListener.java
bus.ticketer.objects.Ticket.java
bus.ticketer.passenger.BeamActivity.java
bus.ticketer.passenger.BusTicketer.java
bus.ticketer.passenger.CentralActivity.java
bus.ticketer.passenger.MainActivity.java
bus.ticketer.runnable.BuyRunnable.java
bus.ticketer.runnable.ShowRunnable.java
bus.ticketer.runnable.TimerRunnable.java
bus.ticketer.runnable.TimerThread.java
bus.ticketer.utils.BusTimer.java
bus.ticketer.utils.BusUtils.java
bus.ticketer.utils.FileHandler.java
bus.ticketer.utils.FileWriter.java
bus.ticketer.utils.Method.java
bus.ticketer.utils.Method.java
bus.ticketer.utils.Method.java
bus.ticketer.utils.RESTFunction.java
bus.ticketer.utils.RESTFunction.java
bus.ticketer.utils.RESTFunction.java
bus.ticketer.utils.SpinnerAdapter.java
bus.ticketer.validation.MainActivity.java
bus.ticketer.validation.ResultActivity.java