Android Open Source - 2014-Droid-code Https Example P O S T






From Project

Back to project page 2014-Droid-code.

License

The source code is released under:

GNU General Public License

If you think the Android project 2014-Droid-code 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 ca.campbell.httpexamplepost;
/*from w  w  w.j a  v a  2  s .co m*/
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.security.cert.Certificate;

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




import ca.campbell.httpexample.R;
/*
import org.json.JSONArray;
import org.json.JSONObject;
 */
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


/*
 * HttpURLConnectionExample
 * 
 * This app sends a URL uses HttpURLConnection class + POST 
 * to download a JSON result and display some of it in a jsonResult
 * I don't parse the JSON, I leave that up to you  
 * remember: object = {}  array = []  
 * 
 * The api used is api.edamam.com, using Java 6 org.json 
 * http://www.json.org/java/
 * note GSON is nicer to use if you want
 * https://sites.google.com/site/gson/gson-user-guide
 * 
 * LAB-LAB-LAB-LAB:
 * If you are looking at this code in order to do the lab the following must be done:
 * 
 * 1.  Fix the Show Certs, it crashes, look in the log and discover why, then fix it!
 * 2.  Deconstruct the JSON returned.  Using either GSON or the JSON classes and read the number of calories,
 *     display the number of calories and what ever other info you want.
 * 
 * 3.  (Optional)  the ingr is an array, modify the app to read several ingredients then get the returned info
 *     for that "recipe".
 * 
 */
public class HttpsExamplePOST extends Activity {
  private static final String TAG = "HttpURLPOST";
  /* 
   * Strings used for the api http://www.edamam.com 
   * 
   *  n.b. do not hard code an api key it should be in a config file
   *  
   *  This is my key and my appid 
   *  GET YOUR OWN if you will be using this in your app
   */
  private static final String APPKEY = "71b785fe9091261dbe192db334d77dbd";
  private static final String APPID = "e6501390";
  private static final String APIURL ="https://api.edamam.com/api/nutrient-info";
  
  private String  urlString, jsonData;
  URL url;
  private EditText foodIngr;
  private TextView jsonResult, ingr;
  /* 
   * you may use these classes to build & deconstruct JSON
  private JSONObject foodJson;
  private JSONArray ingrJson;
   */
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_http_example);

    foodIngr = (EditText) findViewById(R.id.food);
    jsonResult = (TextView) findViewById(R.id.jsonResult);
    ingr = (TextView) findViewById(R.id.ingr); 

    // check: encode only the parms, POST data and URL are ok    
    try {
      urlString = APIURL + "?extractOnly&app_id=" 
          +  URLEncoder.encode(APPID, "UTF-8") 
          + "&app_key=" +  URLEncoder.encode(APPKEY, "UTF-8") ;
    } catch (UnsupportedEncodingException e) {
      jsonResult.setText("Unable to encode params for URL, contact developer");
      Log.e(TAG, e.getMessage());
    }
    Log.d(TAG, "API URL: " + urlString);
  }

  /*
   * When user clicks Load it Button, we use the data and 
   * executes an AsyncTask to do the download. Before
   * attempting to fetch the URL, makes sure that there is a network
   * connection.
   */
  public void clickHandler(View view) {
    // Gets the data to look up  from the UI's text field. 
    String foodInfo = foodIngr.getText().toString();
    if (foodInfo.isEmpty()) 
      foodInfo = "1 tablespoon Butter";

    jsonData =  "{ \"title\": \"Check Food\", "
        + " \"ingr\": [  \"" + foodInfo + "\" ] }";

    ingr.setText("Food: " + foodInfo);
    
    // first check to see if we can get on the network
    ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
      // invoke the AsyncTask to do the dirty work.
      new DownloadFoodData().execute(urlString, jsonData);
    } else {
      jsonResult.setText("No network connection available.");
    }
  } // clickHandler()

  /*
   * Uses AsyncTask to create a task away from the main UI thread. This task
   * takes a URL string and uses it to create an HttpsUrlConnection. Once the
   * connection has been established, the AsyncTask downloads the contents of
   * the webpage via an an InputStream. The InputStream is converted into a
   * string, which is displayed in the UI by the AsyncTask's onPostExecute
   * method.
   */

  private class DownloadFoodData extends AsyncTask<String, Void, String> {

    // onPreExecute log some info
    // runs in calling thread (in UI thread)
    protected void onPreExecute() {
      Log.d(TAG, "url " + urlString);
      Log.d(TAG, "json " + jsonData);

    }
    
    // onPostExecute displays the results of the AsyncTask.
    // runs in calling thread (in UI thread)
    protected void onPostExecute(String result) {
      jsonResult.setText(result);
    }

    @Override
    // runs in background (not in UI thread)
    protected String doInBackground(String... params) {
      // params comes from the execute() call: params[0] is the url.
      try {
        return downloadData(params);
      } catch (IOException e) {
        return "Unable to retrieve web page. URL may be invalid. "  +e.getMessage();
      }
    }
  } // AsyncTask DownloadFoodData()

  /*
   * Given a URL, establishes an HttpUrlConnection and retrieves the web page
   * content as a InputStream, which it returns as a string.
   */

  private String downloadData(String... params) throws IOException {
    InputStream is = null;
    OutputStream out;
    String contentAsString ="";
    int response;
    URL url;
    HttpsURLConnection conn = null;

    byte[] bytes = params[1].getBytes("UTF-8");
    Integer bytesLeng = bytes.length;


    try {      
      url =  new URL(params[0]);
    } catch (MalformedURLException e) {
      Log.d(TAG, e.getMessage());
      return "ERROR call the developer: " + e.getMessage();
    }
    try {
      // create and open the connection
      conn = (HttpsURLConnection) url.openConnection();

      // output = true, uploading POST data
      // input = true, downloading response to POST
      conn.setDoOutput(true);
      conn.setDoInput(true);
      conn.setRequestMethod("POST");

      // conn.setFixedLengthStreamingMode(params[1].getBytes().length);
      // send body unknown length
      //          conn.setChunkedStreamingMode(0);
      conn.setReadTimeout(10000);
      conn.setConnectTimeout(15000 /* milliseconds */);

      conn.setRequestProperty("Content-Type", 
          "application/json; charset=UTF-8");
      // set length of POST data to send
      conn.addRequestProperty("Content-Length", bytesLeng.toString());

      //send the POST out
      out = new BufferedOutputStream(conn.getOutputStream());

      out.write(bytes);
      out.flush();
      out.close();

      // logCertsInfo(conn);

      // now get response
      response = conn.getResponseCode();

      /*  
       *  check the status code HTTP_OK = 200 anything else we didn't get what
       *  we want in the data.
       */
      if (response != HttpURLConnection.HTTP_OK) {
        Log.d(TAG, "Server returned: " + response + " aborting read.");
        return "Server returned: " + response + " aborting read.";
      }
      is = conn.getInputStream();
      contentAsString = readIt(is);
      return contentAsString;

    } finally {

      // Make sure that the Reader is closed after the app is finished using it.
      if (is != null) 
        try {
          is.close();  
        } catch (IOException ignore) { /* ignore */  }
      //* Make sure the connection is closed after the app is finished using it.
      if (conn != null)
        try {

          conn.disconnect();
        } catch (IllegalStateException ignore ) { /* ignore  */ }

    }
  }
  /*
   * Reads stream from HTTP connection and converts it to a String. See
   * stackoverflow or a good explanation of why I did it this way.
   * http://stackoverflow
   * .com/questions/3459127/should-i-buffer-the-inputstream
   * -or-the-inputstreamreader
   */
  public String readIt(InputStream stream) throws IOException,
     UnsupportedEncodingException {
    
    String buffer = "";
    BufferedReader reader = null;
    
    reader =    
        new BufferedReader(new InputStreamReader(stream,"UTF-8"));  

    String line = null;  
    while ((line = reader.readLine()) != null) {  
      // could use string builder   sb.append(line + "\n");
      Log.d(TAG, line);
      // the \n is for display, if I'm parsing the JSON I don't want it
      buffer += line + "\n";
    }  
    return  buffer;
  } // readIt()
  /*
   * Click Listener for the Show Certs button
   * It will connect to the website then get the Certificate data to display and log
   * This will crash, look at the log to see why, then fix it.
   */

  public void showCerts(View v) {
    URL url = null;
    HttpsURLConnection conn = null;
    try {
      url =  new URL(urlString);
    } catch (MalformedURLException e) {
      Log.d(TAG, e.getMessage());
      jsonResult.setText("ERROR call the developer: " + e.getMessage());
      return;
    }
    if (url != null) {

      try {
        conn = (HttpsURLConnection) url.openConnection();
        jsonResult.setText(logCertsInfo(conn));
      } catch (IOException e) {
        Log.d(TAG, e.getMessage());
        jsonResult.setText("ERROR call the developer: " + e.getMessage());
      }
      if (conn != null)
        try {
          //* Make sure the connection is closed after the app is finished using it.
          conn.disconnect();
        } catch (IllegalStateException ignore ) { /* ignore  */ }

    }
  } //showCerts
  
  private String logCertsInfo(HttpsURLConnection conn) {
    String line = "";
    if (conn == null)
      return "No connection";
    try {
      line += "Response Code : " + conn.getResponseCode() + "\n";
      Log.d(TAG, "Response Code : " + conn.getResponseCode());
      line += "Response Code : " + conn.getCipherSuite() + "\n";
      Log.d(TAG, "Cipher Suite : " + conn.getCipherSuite());
      line += "See log for certificates.";

      Certificate[] certs = conn.getServerCertificates();
      for(Certificate cert : certs){
        Log.d(TAG, "Cert Type : " + cert.getType());
        Log.d(TAG, "Cert Hash Code : " + cert.hashCode());
        Log.d(TAG, "Cert Public Key Algorithm : " 
            + cert.getPublicKey().getAlgorithm());
        Log.d(TAG, "Cert Public Key Format : " 
            + cert.getPublicKey().getFormat());
      }
      Log.d(TAG, "End Certs");
    } catch (SSLPeerUnverifiedException e) {
      Log.d(TAG, e.getMessage());
      line += e.getMessage();
    } catch (IOException e){
      Log.d(TAG, e.getMessage());
      line += e.getMessage();
    }
    return line;
  } // logCertsInfo()

} // MainActivity class





Java Source Code List

ca.campbell.httpexample.HttpExample.java
ca.campbell.httpexamplepost.HttpsExamplePOST.java
ca.campbell.layoutprogrammatically.MainActivity.java
ca.campbell.networkcheckstatus.MainActivity.java
ca.campbell.optionsmenu.Activity2.java
ca.campbell.optionsmenu.Activity2.java
ca.campbell.optionsmenu.Activity3.java
ca.campbell.optionsmenu.MainActivity.java
ca.campbell.optionsmenu.MainActivity.java
ca.campbell.simplegridview.MainActivity.java
ca.campbell.week2_rw_views.Activity2.java
ca.campbell.week2_rw_views.MainActivity.java
com.androidbook.simpleasync.ChoiceActivity.java
com.androidbook.simpleasync.SimpleAsyncActivity.java
com.androidbook.simpleasync.SimpleNoBGThread.java
com.androidbook.simpleasync.SimpleThreadActivity.java
com.cookbook.internet.search.GoogleSearch.java
com.introtoandroid.simplefragments.FieldNoteListFragment.java
com.introtoandroid.simplefragments.FieldNoteViewActivity.java
com.introtoandroid.simplefragments.FieldNoteWebViewFragment.java
com.introtoandroid.simplefragments.SimpleFragmentsActivity.java
com.introtoandroid.simplelayout.FrameLayoutActivity.java
com.introtoandroid.simplelayout.GridLayoutActivity.java
com.introtoandroid.simplelayout.LinearLayoutActivity.java
com.introtoandroid.simplelayout.MenuActivity.java
com.introtoandroid.simplelayout.MultipleLayoutActivity.java
com.introtoandroid.simplelayout.RelativeLayoutActivity.java
com.introtoandroid.simplelayout.SimpleLayoutActivity.java
com.introtoandroid.simplelayout.TableLayoutActivity.java
com.introtoandroid.viewsamples.ButtonsActivity.java
com.introtoandroid.viewsamples.ContainersActivity.java
com.introtoandroid.viewsamples.EventsActivity.java
com.introtoandroid.viewsamples.FormsActivity.java
com.introtoandroid.viewsamples.IndicatorsActivity.java
com.introtoandroid.viewsamples.MenuActivity.java
com.introtoandroid.viewsamples.PickersActivity.java
com.introtoandroid.viewsamples.TextDisplayActivity.java
com.introtoandroid.viewsamples.TextInputActivity.java
com.introtoandroid.viewsamples.ViewSampleActivity.java
cs518.sample.activityLifecycle.Activity2.java
cs518.sample.activityLifecycle.MyActivityLifeCycleActivity.java
cs518.sample.database.AddStudent.java
cs518.sample.database.DBHelper.java
cs518.sample.database.DatabaseActivity.java
cs518.sample.database.Thirty.java
cs518.sample.dbcursoradapter.AddStudent.java
cs518.sample.dbcursoradapter.DBHelper.java
cs518.sample.dbcursoradapter.DatabaseActivity.java
cs518.sample.dbcursoradapter.Thirty.java
cs518.sample.localisation.MainActivity.java
cs518.sample.multiactivity.Activity1.java
cs518.sample.multiactivity.Activity2.java
cs518.sample.multiactivity.Activity3.java
cs518.sample.multiactivity.Activity4.java
cs518.sample.multiactivity.Activity5.java
cs518.sample.multiactivity.Activity6.java
cs518.sample.multiactivity.Constants.java
cs518.sample.usecalendarcontentprovider.MainActivity.java
cs518.sample.usecontactcontentprovider.MainActivity.java
cs518.sample.usecontactcontentprovidercursorloader2.MainActivity.java
cs518.sample.usemediastorecontentprovider.MainActivity.java
cs518.samples.imageswap.MainActivity.java
cs518.samples.sharedpreferences.MainActivity.java
cs534.sample.dbAsyncTask.AddStudent.java
cs534.sample.dbAsyncTask.DBHelper.java
cs534.sample.dbAsyncTask.DatabaseActivity.java
cs534.sample.dbAsyncTask.Thirty.java
cs534.sample.implicitintents.MainActivity.java
cs534.sample.multithread.MultiThread.java
cs534.sample.simplelistview.SimpleLV.java
cs534.samples.simplestlv.MainActivity.java