Android Open Source - TumblifeForAndroid T L Connection






From Project

Back to project page TumblifeForAndroid.

License

The source code is released under:

GNU General Public License

If you think the Android project TumblifeForAndroid 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 jp.mitukiii.tumblife.util;
// ww  w  .  j a  v a2 s .  c  o  m
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class TLConnection
{
  public static String POST = "POST";
  public static String GET  = "GET";

  public static HttpURLConnection request(String _url, String method, Map<String, String> parameters, Map<String, String> headers)
    throws MalformedURLException, IOException
  {
    TLLog.i("TLConnection / request : url / " + _url);

    HttpURLConnection con = null;
    OutputStreamWriter writer = null;

    try {
      StringBuilder sb = new StringBuilder();
      Iterator<String> pit = parameters.keySet().iterator();
      while (pit.hasNext()) {
        String parameterKey = (String)pit.next();
        String parameterValue = parameters.get(parameterKey);
        if (sb.length() > 0) {
          sb.append("&");
        }
        sb.append(parameterKey);
        sb.append("=");
        sb.append(URLEncoder.encode(parameterValue));
      }

      if (POST.equals(method)) {
        con = (HttpURLConnection) new URL(_url).openConnection();
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setRequestMethod(method);
        writer = new OutputStreamWriter(con.getOutputStream());
        writer.write(sb.toString());
        writer.flush();
      } else {
        con = (HttpURLConnection) new URL(_url + '?' + sb.toString()).openConnection();
        con.setDoInput(true);
        con.setRequestMethod(method);
      }

      Iterator<String> hit = headers.keySet().iterator();
      while (hit.hasNext()) {
        String field = (String)hit.next();
        String value = headers.get(field);
        con.setRequestProperty(field, value);
      }

      con.connect();

      return con;
    } finally {
      try {
        if (writer != null) {
          writer.close();
        }
      } catch (IOException e) {
        TLLog.i("TLConnection / request", e);
      }
    }
  }

  public static HttpURLConnection request(String _url, String method, Map<String, String> parameters)
    throws MalformedURLException, IOException
  {
    return request(_url, method, parameters, new HashMap<String, String>());
  }

  public static HttpURLConnection request(String _url, String method)
    throws MalformedURLException, IOException
  {
    return request(_url, method, new HashMap<String, String>(), new HashMap<String, String>());
  }

  public static HttpURLConnection post(String _url, Map<String, String> parameters, Map<String, String> headers)
    throws MalformedURLException, IOException
  {
    return request(_url, POST, parameters, headers);
  }

  public static HttpURLConnection post(String _url, Map<String, String> parameters)
    throws MalformedURLException, IOException
  {
    return request(_url, POST, parameters, new HashMap<String, String>());
  }

  public static HttpURLConnection post(String _url)
    throws MalformedURLException, IOException
  {
    return request(_url, POST, new HashMap<String, String>(), new HashMap<String, String>());
  }

  public static HttpURLConnection get(String _url, Map<String, String> parameters, Map<String, String> headers)
    throws MalformedURLException, IOException
  {
    return request(_url, GET, parameters, headers);
  }

  public static HttpURLConnection get(String _url, Map<String, String> parameters)
    throws MalformedURLException, IOException
  {
    return request(_url, GET, parameters, new HashMap<String, String>());
  }

  public static HttpURLConnection get(String _url)
    throws MalformedURLException, IOException
  {
    return request(_url, GET, new HashMap<String, String>(), new HashMap<String, String>());
  }
}




Java Source Code List

jp.mitukiii.tumblife.App.java
jp.mitukiii.tumblife.HardkeySetting.java
jp.mitukiii.tumblife.KeyCodeMap.java
jp.mitukiii.tumblife.Main.java
jp.mitukiii.tumblife.Setting.java
jp.mitukiii.tumblife.exeption.TLAuthenticationFailureException.java
jp.mitukiii.tumblife.exeption.TLFailureException.java
jp.mitukiii.tumblife.exeption.TLParserException.java
jp.mitukiii.tumblife.exeption.TLSDCardNotFoundException.java
jp.mitukiii.tumblife.model.TLModel.java
jp.mitukiii.tumblife.model.TLPost.java
jp.mitukiii.tumblife.model.TLSetting.java
jp.mitukiii.tumblife.model.TLTumblelog.java
jp.mitukiii.tumblife.model.TLUser.java
jp.mitukiii.tumblife.parser.TLParser.java
jp.mitukiii.tumblife.parser.TLPostParser.java
jp.mitukiii.tumblife.parser.TLUserParser.java
jp.mitukiii.tumblife.tumblr.TLDashboardDelegate.java
jp.mitukiii.tumblife.tumblr.TLDashboardInterface.java
jp.mitukiii.tumblife.tumblr.TLDashboard.java
jp.mitukiii.tumblife.ui.TLWebViewClientDelegate.java
jp.mitukiii.tumblife.ui.TLWebViewClient.java
jp.mitukiii.tumblife.util.TLBrowser.java
jp.mitukiii.tumblife.util.TLConnection.java
jp.mitukiii.tumblife.util.TLExplorer.java
jp.mitukiii.tumblife.util.TLLog.java
jp.mitukiii.tumblife.util.TLPostFactory.java