Android Open Source - mitlocate M I T Https Client






From Project

Back to project page mitlocate.

License

The source code is released under:

MIT License

If you think the Android project mitlocate 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 edu.mit.locate;
// ww  w .  j a  v a 2 s.com
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.cert.Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLPeerUnverifiedException;
import javax.net.ssl.SSLSession;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.util.Log;

public class MITHttpsClient extends AsyncTask<JSONObject, Void, JSONObject> {
  private Context ctx;
  private String endpoint;
  private boolean showAlerts;
  private static final String TAG = "MITHttpsClient";

  protected MITHttpsClient(String mEndpoint, Context mCtx, boolean mShowAlerts){
    ctx = mCtx;
    endpoint = mEndpoint;
    showAlerts = mShowAlerts;
  }
  
  class MITHostnameVerifier implements HostnameVerifier {
    @Override
        public boolean verify(String urlHost, SSLSession sslSession){
//      Log.d(TAG, "urlHost: " + urlHost);
//      Log.d(TAG, "peerHost: " + sslSession.getPeerHost() + " on port " + sslSession.getPeerPort());
//      try {
//        Certificate[] certs = sslSession.getPeerCertificates();
//        for(Certificate cert : certs){
//          Log.d(TAG, "Peer "+cert.toString());
//        }
//      } catch (SSLPeerUnverifiedException e) {
//        e.printStackTrace();
//      }
            return true;
        }
    }

  @Override
  protected JSONObject doInBackground(JSONObject... jsonParams){
    if(!isNetworkAvailable()){
      if(showAlerts){
        ((Activity) ctx).runOnUiThread(new Runnable(){
          @Override
          public void run() {
            new AlertDialog.Builder(ctx)
            .setMessage("No Internet Connection")
            .setPositiveButton("OK", null)
            .setTitle("Error").show();
          }
        });
      } else {
        Log.e(TAG, "No internet connection!");
      }
      return null;
    } else {
      HttpsURLConnection con = null;
      StringBuilder content = new StringBuilder();
      try {
        URL url = new URL("https://locate.mit.edu/api/" + endpoint);
        con = (HttpsURLConnection)url.openConnection();
        con.setDoOutput(true);
        con.setDoInput(true);
        con.setHostnameVerifier(new MITHostnameVerifier());
        con.setRequestMethod("POST");
        //con.setRequestProperty("User-Agent", "GYUserAgentAndroid");
        con.setRequestProperty("Content-Type", "application/json; charset=utf8");
        con.setRequestProperty("Accept", "application/json");
        con.setUseCaches(false);

        con.setConnectTimeout(5000);
        con.setReadTimeout(5000);

        con.connect();

        OutputStream out = con.getOutputStream();
        out.write(jsonParams[0].toString().getBytes("UTF-8"));
        out.close();
        Log.w(TAG, "sent " + jsonParams[0].toString());

        final int status = con.getResponseCode();

        BufferedReader br;
        if(status==200){
          br = new BufferedReader(new InputStreamReader(con.getInputStream()));
        }else{
          br = new BufferedReader(new InputStreamReader(con.getErrorStream()));
          String line = "";
          while ((line = br.readLine()) != null) {
            content.append(line);
          }
          Log.e(TAG, content.toString());
          return null;
        }
        String line = "";
        while ((line = br.readLine()) != null) {
          content.append(line);
        }
        br.close();
      }catch(MalformedURLException e){
        e.printStackTrace();
      }catch(IOException e){
        e.printStackTrace();
        Log.e(TAG, "Connection failed.");
        return null;
      }finally{
        if(con!=null)
          con.disconnect();
      }
      try {
        return new JSONObject(content.toString());
      } catch (JSONException e) {
        e.printStackTrace();
        return null;
      }
    }
  }

  @Override
  protected void onPostExecute(JSONObject response){
    Log.w(TAG, "Got response "+response);
  }

  private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
  }
}




Java Source Code List

edu.mit.locate.APEditorActivity.java
edu.mit.locate.BootLocationService.java
edu.mit.locate.LoginActivity.java
edu.mit.locate.MITHttpsClient.java
edu.mit.locate.MITLocationService.java
edu.mit.locate.MainActivity.java
edu.mit.locate.alerts.Alert.java
edu.mit.locate.alerts.AlertsListAdapter.java
edu.mit.locate.alerts.AlertsTab.java
edu.mit.locate.apeditor.APListAdapter.java
edu.mit.locate.apeditor.APScanResult.java
edu.mit.locate.friends.FriendActivity.java
edu.mit.locate.friends.Friend.java
edu.mit.locate.friends.FriendsListAdapter.java
edu.mit.locate.friends.FriendsTab.java
edu.mit.locate.tabs.IconPagerAdapter.java
edu.mit.locate.tabs.IcsLinearLayout.java
edu.mit.locate.tabs.NavigationTab.java
edu.mit.locate.tabs.PageIndicator.java
edu.mit.locate.tabs.SearchTab.java
edu.mit.locate.tabs.SettingsTab.java
edu.mit.locate.tabs.TabPageIndicator.java
edu.mit.locate.tabs.TabPagerAdapter.java