java.net.ProtocolException: Connection already established ( Not Found
Proper Solution in existing Post )
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.
No comments:
Post a Comment