Hi everybody,

for some time now, I'm facing a problem I can't solve.

I'm having a method getJSONObject (see below) that - given a url as
the parameter - returns the requested JSON object. I'm connecting to
an API, but that's not important to know, as the mistake won't be
there (I tested it a lot of times with a REST client, the API works
fine).

Now the problem that appears is the following:
Whenever I use the method below for the first time, it works all fine.
Even within a certain time span, it still works. So, for example, if I
use getJSONObject a second / third / ... time within the next seconds,
it all still works fine. BUT: When I wait (let's say) a minute and run
getJSONObject again, a connection cannot be established anymore. Via
getResponseCode() I get "-1". And even more strange: After that, when
I run getJSONObject again, it all works fine again.

I hope I described the problem well enough - And I so hope you can
help me!

Thank you for your time!

[code]
       public static JSONObject getJSONObject(String url)
       {
          try
          {
             SSLContext sslContext = SSLContext.getInstance("TLS");
             sslContext.init(null, new TrustManager[] { new
MyTrustManager() }, new SecureRandom());

 
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
             HttpsURLConnection.setDefaultHostnameVerifier(new
MyHostnameVerifier());
             HttpsURLConnection httpsUrlConnection =
(HttpsURLConnection) new URL(url).openConnection();
             httpsUrlConnection.setRequestMethod("GET");
 
httpsUrlConnection.addRequestProperty(ApiData.getHeader1(),
ApiData.getHeader1Value());
             httpsUrlConnection.setDoOutput(true);
             httpsUrlConnection.setDoInput(true);
             httpsUrlConnection.connect();

             BufferedReader bufferedReader = new BufferedReader(new
InputStreamReader(httpsUrlConnection.getInputStream()));

             StringBuffer stringBuffer = new StringBuffer();

             String readLine;

             while((readLine = bufferedReader.readLine()) != null)
             {
                stringBuffer.append(readLine);
             }

             bufferedReader.close();

             httpsUrlConnection.disconnect();

             return new JSONObject(stringBuffer.toString());
          }
          catch(Exception e)
          {
             e.printStackTrace();
          }

          return null;
       }
[/code]

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to