In one of my android app, i am getting error "java.net.ProtocolException: Connection already established" during call the API. (I refered "Connection already established" exception in HttpsURLConnection for solution but not useful)
Below is my code to call API. Code is working for all the API except the one. That API is the same way developed as the Others and still below code throwing error for that API.
HttpURLConnection con = null;
BufferedReader in = null;
StringBuilder sb = new StringBuilder();
try {
URL uri = null;
uri = new URL(url);
LogUtil.v("~~URL - "+url);
con = (HttpURLConnection) uri.openConnection();
System.setProperty("http.keepAlive", "false");
con.setRequestMethod(requestType); //type: POST, PUT, DELETE, GET
con.setDoOutput(true);
// con.setDoInput(true);
con.setConnectTimeout(TIMEOUT); //20 secs
con.setReadTimeout(TIMEOUT); //20 secs
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
String authorizationString = "Basic " + Base64.encodeToString((myAuthString).getBytes(), Base64.DEFAULT);
con.setRequestProperty("Authorization", authorizationString);
if( body != null){
LogUtil.v("~~BODY - "+body);
DataOutputStream out = new DataOutputStream(con.getOutputStream());
out.writeBytes(body);
out.flush();
out.close();
}
con.connect();
InputStream inst = con.getInputStream();
in = new BufferedReader(new InputStreamReader(inst));
String temp = null;
while((temp = in.readLine()) != null){
sb.append(temp).append(" ");
}
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally{
if(in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(cb!=null){
if (sb.toString().length() >0) {
LogUtil.v("~~ RESPONSE - "+ sb.toString());
cb.onResponse(sb.toString(), actionCode);
}else{
LogUtil.v("~~ RESPONSE - null");
cb.onResponse(null, actionCode);
}
}
if(con!=null)
con.disconnect();
}
Please don't suggest to use DefaultHttpClient here.
I want to solve the problem using above BECAUSE IT IS WORKING FOR ALL OTHER APIS.
FYI: API CALL IS ALREADY CHECKED USING BROWSER AND IT IS RETURNING RETURNING DATA. IT IS JUST THROWING ERROR WHEN CALLING FROM ANDROID.
~~~~~~ EDITED ~~~~~~~~~ (SERVER SIDE APIS ARE DEVELOPED IN PHP)
Let me simplify my problem explanation:
function API1Call(){
// callAPI(param...)
}
function API2Call(){
// callAPI(param...)
}
callAPI(param..){
// above API call code goes here
}
if you see the function callAPI(param...), it executing whenever required to call API.
***> Now in my proj, i m calling many apis written on different activites.
Only for One API call, above callAPI(param...) throwing exception that i mentioned above.***
~~~~~ MY LOGCAT ~~~~~~~
08-31 14:22:45.309: W/System.err(27364): java.net.ProtocolException: Connection already established
08-31 14:22:45.314: W/System.err(27364): at java.net.HttpURLConnection.setRequestMethod(HttpURLConnection.java:666)
08-31 14:22:45.324: W/System.err(27364): at libcore.net.http.HttpsURLConnectionImpl.setRequestMethod(HttpsURLConnectionImpl.java:144)
08-31 14:22:45.329: W/System.err(27364): at
com.indapoint.ewe.api.EWeAPI$1.run(EWeAPI.java:245)
08-31 14:22:45.334: W/System.err(27364): at java.lang.Thread.run(Thread.java:856)