http request does not work for android version 4 -
i run application android version 2 , works ok, when run version 4 appears exception in line request.seturi(new uri(url));
public string getxmlfromurl(string url) { string xml = null; try { // defaulthttpclient defaulthttpclient client = new defaulthttpclient(); httpget request = new httpget(); try { request.seturi(new uri(url)); } catch (urisyntaxexception e) { // todo auto-generated catch block e.printstacktrace(); system.out.print("http\n"); } httpresponse httpresponse = client.execute(request); httpentity httpentity = httpresponse.getentity(); xml = entityutils.tostring(httpentity); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } // return xml return xml; }
i can not understand problem.
try below code. have use sslsocketfactory
put after setcontentview
int sdk_int = android.os.build.version.sdk_int; } if (sdk_int>8){ strictmode.threadpolicy policy = new strictmode.threadpolicy.builder().permitall().build(); strictmode.setthreadpolicy(policy); }
httpclient httpclient = getnewhttpclient();
.... // httpget request = new httpget(); ..
public httpclient getnewhttpclient() { try { keystore truststore = keystore.getinstance(keystore .getdefaulttype()); truststore.load(null, null); sslsocketfactory sf = new mysslsocketfactory(truststore); sf.sethostnameverifier(sslsocketfactory.allow_all_hostname_verifier); httpparams params = new basichttpparams(); httpprotocolparams.setversion(params, httpversion.http_1_1); httpprotocolparams.setcontentcharset(params, http.utf_8); schemeregistry registry = new schemeregistry(); registry.register(new scheme("http", plainsocketfactory .getsocketfactory(), 80)); registry.register(new scheme("https", sf, 443)); clientconnectionmanager ccm = new threadsafeclientconnmanager( params, registry); return new defaulthttpclient(ccm, params); } catch (exception e) { return new defaulthttpclient(); } }
mysslsocketfactory.java
public class mysslsocketfactory extends sslsocketfactory { sslcontext sslcontext = sslcontext.getinstance("tls"); public mysslsocketfactory(keystore truststore) throws nosuchalgorithmexception, keymanagementexception, keystoreexception, unrecoverablekeyexception { super(truststore); trustmanager tm = new x509trustmanager() { public void checkclienttrusted(x509certificate[] chain, string authtype) throws certificateexception { } public void checkservertrusted(x509certificate[] chain, string authtype) throws certificateexception { } public x509certificate[] getacceptedissuers() { return null; } }; sslcontext.init(null, new trustmanager[] { tm }, null); } @override public socket createsocket(socket socket, string host, int port, boolean autoclose) throws ioexception, unknownhostexception { return sslcontext.getsocketfactory().createsocket(socket, host, port, autoclose); } @override public socket createsocket() throws ioexception { return sslcontext.getsocketfactory().createsocket(); } }
Comments
Post a Comment