I'm getting the below exception when I run TestUtil class from android studio. But if I run it from eclipse it works fine.
java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java:189) at java.net.SocketInputStream.read(SocketInputStream.java:121) at sun.security.ssl.InputRecord.readFully(InputRecord.java:442) at sun.security.ssl.InputRecord.read(InputRecord.java:480) at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:927) at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323) at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:515) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1299) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254) at com.XYZ.android.cmpi.util.RestUtil.get(RestUtil.java:36) at com.XYZ.android.cmpi.tests.TestUtil.main(TestUtil.java:15)
public class TestUtil {
public static void main(String[] args) throws MalformedURLException {
String response = RestUtil.get(new URL(RestUtil.getURL()));
print(response);
}
public static void print(Object o) {
out.print(o);
}
}
//
public class RestUtil {
public final static String USER_AGENT = "Mozilla/5.0";
public static String get(URL url) {
StringBuilder sb = new StringBuilder();
HttpURLConnection connection = null;
try {
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
BufferedReader br = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
connection.disconnect();
}
return sb.toString();
}
public static String getURL() {
// returns URL
}
}
I'm not sure why I'm getting this exception when I run from android studio, but not from Eclipse.