android - HttpURLConnection GET method works in api 22 only in debug -
httpurlconnection
method works in api 22 in debug mode, while in other apis works properly. works in approximately in 3 of 4 cases fires exception:
e/http httpurlconnection:﹕ 2015/05/15 13:18:04 failed connect /10.254.254.1 (port 80) after 5000ms
this code:
try { url url = new url(path); urlconnection = (httpurlconnection) url.openconnection(); urlconnection.setinstancefollowredirects(false); urlconnection.setconnecttimeout(5000); urlconnection.setreadtimeout(5000); urlconnection.setrequestmethod("get"); urlconnection.setrequestproperty("user-agent", user_agent); urlconnection.setrequestproperty("content-type", "text/plain;charset=utf-8"); int responsecode = urlconnection.getresponsecode(); mfunctions.addlog(3, "http responsecode: ", integer.tostring(responsecode)); bufferedreader in = new bufferedreader( new inputstreamreader(urlconnection.getinputstream())); string inputline = in.readline(); return inputline; } catch (malformedurlexception e) { mfunctions.addlog(3, "http url: ", e); } catch (ioexception e) { mfunctions.addlog(3, "http httpurlconnection: ", e); } { urlconnection.disconnect(); }
i tryed use httpok
instead of httpurlconnection
, time problem same on api 22 , exception:
6371-6611/kepardvpn.client w/system.err﹕ java.net.sockettimeoutexception: failed connect /10.254.254.1 (port 80) after 15000ms 05-15 20:05:52.364 6371-6611/kepardvpn.client w/system.err﹕ @ libcore.io.iobridge.connecterrno(iobridge.java:169) 05-15 20:05:52.364 6371-6611/kepardvpn.client w/system.err﹕ @ libcore.io.iobridge.connect(iobridge.java:122) 05-15 20:05:52.364 6371-6611/kepardvpn.client w/system.err﹕ @ java.net.plainsocketimpl.connect(plainsocketimpl.java:183) 05-15 20:05:52.364 6371-6611/kepardvpn.client w/system.err﹕ @ java.net.plainsocketimpl.connect(plainsocketimpl.java:456) 05-15 20:05:52.364 6371-6611/kepardvpn.client w/system.err﹕ @ java.net.socket.connect(socket.java:882) 05-15 20:05:52.364 6371-6611/kepardvpn.client w/system.err﹕ @ com.squareup.okhttp.internal.platform$android.connectsocket(platform.java:197) 05-15 20:05:52.364 6371-6611/kepardvpn.client w/system.err﹕ @ com.squareup.okhttp.connection.connect(connection.java:156) 05-15 20:05:52.364 6371-6611/kepardvpn.client w/system.err﹕ @ com.squareup.okhttp.connection.connectandsetowner(connection.java:175) 05-15 20:05:52.364 6371-6611/kepardvpn.client w/system.err﹕ @ com.squareup.okhttp.okhttpclient$1.connectandsetowner(okhttpclient.java:120) 05-15 20:05:52.364 6371-6611/kepardvpn.client w/system.err﹕ @ com.squareup.okhttp.internal.http.httpengine.nextconnection(httpengine.java:330) 05-15 20:05:52.364 6371-6611/kepardvpn.client w/system.err﹕ @ com.squareup.okhttp.internal.http.httpengine.connect(httpengine.java:319) 05-15 20:05:52.364 6371-6611/kepardvpn.client w/system.err﹕ @ com.squareup.okhttp.internal.http.httpengine.sendrequest(httpengine.java:241) 05-15 20:05:52.364 6371-6611/kepardvpn.client w/system.err﹕ @ com.squareup.okhttp.call.getresponse(call.java:271) 05-15 20:05:52.364 6371-6611/kepardvpn.client w/system.err﹕ @ com.squareup.okhttp.call$applicationinterceptorchain.proceed(call.java:228) 05-15 20:05:52.364 6371-6611/kepardvpn.client w/system.err﹕ @ com.squareup.okhttp.call.getresponsewithinterceptorchain(call.java:199) 05-15 20:05:52.364 6371-6611/kepardvpn.client w/system.err﹕ @ com.squareup.okhttp.call.execute(call.java:79) 05-15 20:05:52.364 6371-6611/kepardvpn.client w/system.err﹕ @ kepardvpn.client.httpconnections.get(httpconnections.java:196) 05-15 20:05:52.364 6371-6611/kepardvpn.client w/system.err﹕ @ kepardvpn.client.functions$getcurrentiptask.doinbackground(functions.java:1549) 05-15 20:05:52.364 6371-6611/kepardvpn.client w/system.err﹕ @ kepardvpn.client.functions$getcurrentiptask.doinbackground(functions.java:1537) 05-15 20:05:52.364 6371-6611/kepardvpn.client w/system.err﹕ @ android.os.asynctask$2.call(asynctask.java:292) 05-15 20:05:52.364 6371-6611/kepardvpn.client w/system.err﹕ @ java.util.concurrent.futuretask.run(futuretask.java:237) 05-15 20:05:52.365 6371-6611/kepardvpn.client w/system.err﹕ @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) 05-15 20:05:52.365 6371-6611/kepardvpn.client w/system.err﹕ @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) 05-15 20:05:52.365 6371-6611/kepardvpn.client w/system.err﹕ @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) 05-15 20:05:52.365 6371-6611/kepardvpn.client w/system.err﹕ @ java.lang.thread.run(thread.java:818)
this code:
okhttpclient client = new okhttpclient(); client.setconnecttimeout(15, timeunit.seconds); // connect timeout client.setreadtimeout(30, timeunit.seconds); // socket timeout client.networkinterceptors().add(new useragentinterceptor(user_agent)); request request = new request.builder() .url(path) .build(); response response = null; try { response = client.newcall(request).execute(); inputstream in = null; in = response.body().bytestream(); bufferedreader reader = new bufferedreader(new inputstreamreader(in)); string result, line = reader.readline(); //timeunit.milliseconds.sleep(2000); result = line; while ((line = reader.readline()) != null) { result += line; } if (response.code() == 200) return result; } catch (ioexception e) { e.printstacktrace(); }
try this
public static string retrievestream(string link, string parameters) { try { link = link.replace(" ", "%20"); // parameters = parameters.replace(" ", "%20"); url url = new url(link); httpurlconnection connection = (httpurlconnection) url .openconnection(); connection.setdooutput(true); connection.setrequestproperty("content-type", "application/x-www-form-urlencoded"); connection.setrequestmethod("post"); outputstreamwriter request = new outputstreamwriter( connection.getoutputstream()); request.write(parameters); request.flush(); request.close(); string line = ""; inputstreamreader isr = new inputstreamreader( connection.getinputstream()); bufferedreader reader = new bufferedreader(isr); stringbuilder sb = new stringbuilder(); while ((line = reader.readline()) != null) { sb.append(line); } // response server after login process stored in // response variable. string response = sb.tostring(); // can perform ui operations here isr.close(); reader.close(); return response; } catch (ioexception ignored) { ignored.printstacktrace(); } return ""; }
Comments
Post a Comment