0

I'm very new to using web apis and am having trouble getting data in JSON format using Riot Games' API. Here's the code:

public void readJSONFeed(String address) throws IOException {
    URL url = new URL(address);
    JSONObject jsonResponse = null;
    HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.setDoInput(true);
    urlConnection.setDoOutput(false);
    urlConnection.connect(); //Crashes here
    BufferedInputStream in = new BufferedInputStream(urlConnection.getInputStream());

I've marked where the code crashes (it works if you comment everything after and including that). I think the issue might be that the web service I'm using uses https and I don't know how to make the connection secure. I have added the internet permission to the manifest file.

Cœur
  • 37,241
  • 25
  • 195
  • 267
marvelez
  • 3
  • 1
  • 3
  • Have you not already connected with url.openConnection();? – Mark Jun 07 '15 at 22:49
  • Please clarify what exception class is thrown. As Mark Keen mentioned, the `connect()` call is unnecessary, `getInputStream()` will connect automatically. However, if you are on the main thread, any network operation will throw a `NetworkOnMainThreadException`! That would be my first suspect. – vzsg Jun 07 '15 at 23:27
  • I did, however deleting either connection does not fix the error. good catch though, I should have looked more closely before pasting the code. – marvelez Jun 07 '15 at 23:30
  • Yes that is correct, its a NetworkOnMainThreadException. I looked up the error documentation and it looks like I'll be able to figure it out now hopefully. Thanks! – marvelez Jun 07 '15 at 23:46

2 Answers2

0

Have you tried with HttpClient? This is how i connect to backend server:

HttpPost httpPost = new HttpPost(url); //You can use HttpGet
httpPost.setEntity(new UrlEncodedFormEntity(postParameters));

HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams,timeout);
HttpConnectionParams.setSoTimeout(httpParams,timeout);

HttpResponse response = httpClient.execute(httpPost);

String jsonToParse = EntityUtils.toString(response.getEntity());

Where

  • URL: Is the url of the server
  • postParemeters is ArrayList
  • timeout: Integer parameter with time out

If you want to use SSL Connection you can add this code:

SchemeRegistry schemeRegistry = new SchemeRegistry();
                schemeRegistry.register(new Scheme("http",PlainSocketFactory.getSocketFactory(),80));
                schemeRegistry.register(new Scheme("https",
                         SSLSocketFactory.getSocketFactory(), 443));

                HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);

                SingleClientConnManager mgr = new SingleClientConnManager(
                        httpParams, schemeRegistry);
                httpClient = new DefaultHttpClient(mgr, httpParams);
                httpClient.setRoutePlanner(new DefaultHttpRoutePlanner(
                        schemeRegistry));

Hope this works!!

Daniel
  • 3
  • 3
0

It was a NetworkOnMainThread Exception (documentation at https://developer.android.com/reference/android/os/NetworkOnMainThreadException.html).

marvelez
  • 3
  • 1
  • 3