Httpurlconnection with samsung s5 - android

I found an issue in Samsung S5 with using HTTPURLCONNECTION may be I'm doing wrong but if any one have an idea kindly guide me.
Here is my code :
URL urls = new URL(url);
HttpURLConnection connection = (HttpURLConnection) urls.openConnection();
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("x-os-application-token", Constants.APPLICATION_TOKEN);
connection.setConnectTimeout(900000);
connection.setReadTimeout(150000);
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
DataOutputStream wr = new DataOutputStream(
connection.getOutputStream());
wr.writeBytes(json);
wr.flush();
wr.close();
int responseCode = connection.getResponseCode();
Now using this code if I manually Internet disconnect (WI_FI OFF) then other device (e.g. Nexus 5 and Samsung Galaxy Note 3) give me response immediate but In Samsung S5 it respond me after connection.setReadTimeout(150000) ??? 2.5 minutes

Related

Getting a Status Code of 0 when Calling HttpUrlConnection.getResponseCode() in AsyncTask

HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
con.setRequestProperty("Accept", "application/json");
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
wr.write(data.toString());
wr.flush();
status = con.getResponseCode();
This code is part of an AsyncTask class in Android Studio. When I execute a Post request using PostMan, the status returned is 200; however, when I make the request using the android emulator, the status is returned as 0. I'm super confused, and some help would be appreciated.
Status code 0 means some kind of network error.
Try checking for:
Internet permission
Emulator, Connectivity

Android HttpUrlConnection hangs on getResponseCode()

So, from my (android) api I am trying to login to my web service by posting username and password to the login page. The server should then redirect either to a success url, or an error url, depending on whether the the authentication is successful or not.
The code reads as follows:
String urlParameters = "_username=" + URLEncoder.encode(username, "UTF-8")
+ "&_password=" + URLEncoder.encode(password, "UTF-8");
URL url = new URL(MainActivity.I_SAIL_URL + "/member/login_check");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("api", "requestKey");
connection.setConnectTimeout(5000);
//connection.setReadTimeout(5000);
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
//Send request
OutputStream os = connection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(data);
writer.flush();
writer.close();
os.close();
int responseCode = connection.getResponseCode();
// if there is a response code AND that response code is 200 OK, do
// stuff in the first if block
if (responseCode == HttpURLConnection.HTTP_OK) {
//do stuff
}
else {
//do other stuff
}
Now, everything works fine in an android 6.0 emulator, but it doesn't work on an actual device running on 4.1.2. When i uncomment the connection.setReadTimeout(), it will throw a timeout exception, if i don't it will run for about 10 to 15 minutes and then sometimes give me the expected result.
The problem occurs only when the server redirects. If there is no redirect, everything is fine. I have tried HttpUrlConnection.setFollowRedirects(true) as well as connection.setInstanceFollowRedirects(true), to no success.
Any ideas what can cause such a behaviour?

Android upload file to HttpServer using HTTP POST (File type: httppostedfilebase)

I need to upload file to server using httppost type. In server side they received the file in httppostedfilebase instance.
How to do this? Suggest me.
try this:
url = new URL("http://....");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
outputStream = new DataOutputStream( connection.getOutputStream() );
outputStream.writeBytes(stufftosend);
outputStream.flush();
outputStream.close();

SOAP Authentication Android

I am trying to hit this services but this gives me html reponse please help me where i am wrong
any help is really very help ful
HttpURLConnection connection;
java.net.URL u;
u = new java.net.URL("http://sdservices.iyogi.net/iyogi/webservicesnonrestv5/toolbarwebservices.asmx");
URLConnection uc = u.openConnection();
connection = (HttpURLConnection) uc;
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("SOAPAction", "http://sdservices.iyogi.net/SD/ToolbarService.svc/GetProductSettings?SubscriptionCode=W101982488");
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-type", "text/xml; charset=utf-8");
InputStream is = connection.getInputStream();
inputStreamToString(is);
gives HTML in response:(

HttpURLConnection disconnect doesn't work in Android

The method disconnect from HttpURLConnection seems not to work properly.
If I execute the following code:
url = new URL("http:// ...");
connection = (HttpURLConnection) url.openConnection ();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.connect();
// Some code
connection.disconnect();
connection.setDoInput(false); // -> IllegalStateException
I get an IllegalStateException when I call the method setDoInput. The exception says:
Already connected
It sounds like you're trying to reuse the connection? i.e. altering the request properties after you've disconnected from the server, ready to make another connection.
If that is the case, then just create a new HttpURLConnection object.

Categories

Resources