Back to project page BusTicketer.
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.
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; } }