Friday, 15 August 2014

connection to a web service on https:// in android -



connection to a web service on https:// in android -

working on event manager application , connecting http:// web service retrieve json response storing events in local sqlite db , forth going alright , until moved web service https://, started receiving ssl certificate error every time tried connect service . using defaulthttpclient , looked solutions on stack overflow , found going far write custom classes solve issue . can't believe there's no straight forwards solution connect web service on https:// android without having write custom

i'd appreciate if help me issue

here's code

package com.xxxxx.xxxxxxxxx.utility; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.unsupportedencodingexception; import java.util.list; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.httpstatus; import org.apache.http.httpversion; import org.apache.http.namevaluepair; import org.apache.http.statusline; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.responsehandler; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httpget; import org.apache.http.client.methods.httppost; import org.apache.http.client.utils.urlencodedutils; import org.apache.http.conn.clientconnectionmanager; import org.apache.http.conn.scheme.scheme; import org.apache.http.impl.client.basicresponsehandler; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.impl.conn.singleclientconnmanager; import org.apache.http.impl.conn.tsccm.threadsafeclientconnmanager; import org.apache.http.params.basichttpparams; import org.apache.http.params.httpconnectionparams; import org.apache.http.params.httpparams; import org.apache.http.params.httpprotocolparams; import org.apache.http.protocol.http; import org.json.jsonexception; import org.json.jsonobject; import android.util.log; public class jsonparser { // input stream buffer info static inputstream = null; //json object static jsonobject jobj = new jsonobject(); // json string static string json = ""; private string socketresult = null; // constructor public jsonparser() { } // function json url public jsonobject makehttprequest(string url, list<namevaluepair> params) { // making http request seek { final httpparams httpparameters = new basichttpparams(); // set timeout in milliseconds until connection established. httpconnectionparams.setconnectiontimeout(httpparameters, 240000); // set default socket timeout (so_timeout) // in milliseconds timeout waiting data. httpconnectionparams.setsotimeout(httpparameters, 180000); // request method post // defaulthttpclient defaulthttpclient httpclient = new defaulthttpclient(httpparameters); // http post method httppost httppost = new httppost(url); // hands entity request. // thr paramters pass request in form of name value pairs httppost.setentity(new urlencodedformentity(params)); // handels response form http request httpresponse httpresponse = httpclient.execute(httppost); statusline status = httpresponse.getstatusline(); if (status.getstatuscode() == httpstatus.sc_ok) { // entity can sent or received http message. entities can found in requests , in responses, optional. httpentity httpentity = httpresponse.getentity(); // store content / info of entity in input stream = httpentity.getcontent(); }else{ // else, if wanted. } // entity can sent or received http message. entities can found in requests , in responses, optional. // httpentity httpentity = httpresponse.getentity(); // store content / info of entity in input stream //is = httpentity.getcontent(); } grab (unsupportedencodingexception e) { log.d("unsupportedencodingexception", "http error", e); } grab (clientprotocolexception e) { log.d("clientprotocolexception", "http error", e); } grab (ioexception e) { log.d("ioexception", "connection error", e); } seek { // read info in input stream entity buffer bufferedreader reader = new bufferedreader(new inputstreamreader(is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; // build string builder object buffer info while ((line = reader.readline()) != null) { sb.append(line + "\n"); } // close input stream is.close(); // create string json info json = sb.tostring(); } grab (exception e) { log.d("buffer error", "error converting result " + e.tostring()); } // seek parse string json object seek { //---------------------------------------------------------------------- //log.d(" creating json object " , json); jobj = new jsonobject(json); } grab (jsonexception e) { log.d("json parser", "error parsing info " + e.tostring()); //---------------------------------------------------------------------- system.out.println("json parserexception:" + e); } // homecoming json string homecoming jobj; } }

the certificate has signed trusted 3rd party certification authorization recognizes device. if not case, there hacks code include certificate within , validate it.

android web-services ssl https connection

No comments:

Post a Comment