HttpURLConnection.getResponseCode() alway return null pointer exception.

1,520 views
Skip to first unread message

Tran Quoc Anh

unread,
Jul 18, 2014, 6:15:40 AM7/18/14
to rob...@googlegroups.com
Hi, when I try to call getResponseCode method (or connect(),..), it's causes null pointer exception. Here's my code:
  
URL url = new URL("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + accessToken);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
Log.log("con = "+con);
int sc = con.getResponseCode();

When I print con to log, it's not null. But when I call con. getResponseCode() the app crash with null pointer exception.

Plese help me to fix it. Thanks

Tran Quoc Anh

unread,
Jul 20, 2014, 9:19:28 PM7/20/14
to rob...@googlegroups.com
is there anyone have the same problem? please help me to fix it. thanks.

Tran Quoc Anh

unread,
Jul 21, 2014, 12:39:23 AM7/21/14
to rob...@googlegroups.com
If the URL is https (ex: https://twitter.com/), some methods of HttpURLConnection will cause Null Pointer Exception. But if the URL is http (ex: http://www.robovm.org/), it doesn't happen. Plese help me.

Niklas Therning

unread,
Aug 6, 2014, 2:51:16 AM8/6/14
to Tran Quoc Anh, rob...@googlegroups.com
Check out this previous post and make sure you have those patterns in your <forceLinkClasses> in your robovm.xml file: https://groups.google.com/d/msg/robovm/trYc6iv71-Y/e9jdnG54byoJ


On Mon, Jul 21, 2014 at 6:39 AM, Tran Quoc Anh <windlo...@gmail.com> wrote:
If the URL is https (ex: https://twitter.com/), some methods of HttpURLConnection will cause Null Pointer Exception. But if the URL is http (ex: http://www.robovm.org/), it doesn't happen. Plese help me.

--
You received this message because you are subscribed to the Google Groups "RoboVM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robovm+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tran Quoc Anh

unread,
Aug 13, 2014, 3:02:10 AM8/13/14
to rob...@googlegroups.com, windlo...@gmail.com
My app crash with no error in robovm console after add some patterns in my <forceLinkClasses> in my robovm.xml file, like this:

<forceLinkClasses>

<pattern>com.badlogic.gdx.scenes.scene2d.ui.SplitPane$SplitPaneStyle</pattern>

<pattern>org.apache.harmony.security.provider.**</pattern>

<pattern>com.android.org.bouncycastle.jce.provider.**</pattern>

<pattern>org.apache.harmony.xnet.provider.jsse.**</pattern>

<pattern>com.badlogic.gdx.sqlite.robovm.RobovmDatabaseManager</pattern>

<pattern>SQLite.**</pattern>

<!-- For http: support in URLConnection -->

    <pattern>com.android.okhttp.HttpHandler</pattern>

    <!-- For https: support in URLConnection -->

    <pattern>com.android.okhttp.HttpsHandler</pattern>

    <!-- OpenSSL is preferred over BouncyCastle. This pulls in full OpenSSL support. -->

    <pattern>com.android.org.conscrypt.**</pattern>

    <!-- We still need KeyStore.BKS and CertPathValidator.PKIX from BouncyCastle -->

    <pattern>com.android.org.bouncycastle.jce.provider.BouncyCastleProvider</pattern>

    <pattern>com.android.org.bouncycastle.jcajce.provider.keystore.BC$Mappings</pattern>

    <pattern>com.android.org.bouncycastle.jcajce.provider.keystore.bc.BcKeyStoreSpi</pattern>

    <pattern>com.android.org.bouncycastle.jcajce.provider.keystore.bc.BcKeyStoreSpi$Std</pattern>

    <pattern>com.android.org.bouncycastle.jce.provider.PKIXCertPathValidatorSpi</pattern>

    <!-- Use AndroidDigestFactoryOpenSSL as AndroidDigestFactory implementation -->

    <pattern>com.android.org.bouncycastle.crypto.digests.AndroidDigestFactoryOpenSSL</pattern>

      <pattern>java.util.logging.SimpleFormatter</pattern>

    <pattern>java.util.logging.ConsoleHandler</pattern>

  </forceLinkClasses>

Please look my crash file included. Thanks
iWinOnline_2014-08-13-111849_Trans-Mac-mini.crash

Niklas Therning

unread,
Aug 15, 2014, 5:39:11 AM8/15/14
to Tran Quoc Anh, rob...@googlegroups.com
This works just fine for me both in the simulator and on device:

    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    System.out.println(con);
    int sc = con.getResponseCode();
    System.out.println(sc);

Prints out

com.android.okhttp.internal.http.HttpsURLConnectionImpl$HttpUrlConnectionDelegate:https://www.googleapis.com/oauth2/v1/userinfo?access_token=foo
401

Obviously I cannot test this with a valid access token.

I have this in my robovm.xml

  <forceLinkClasses>
    <!-- For http: support in URLConnection -->
    <pattern>com.android.okhttp.HttpHandler</pattern>

    <!-- For https: support in URLConnection -->
    <pattern>com.android.okhttp.HttpsHandler</pattern>
    <!-- OpenSSL is preferred over BouncyCastle. This pulls in full OpenSSL support. -->
    <pattern>com.android.org.conscrypt.**</pattern>
    <!-- We still need KeyStore.BKS and CertPathValidator.PKIX from BouncyCastle -->
    <pattern>com.android.org.bouncycastle.jce.provider.BouncyCastleProvider</pattern>
    <pattern>com.android.org.bouncycastle.jcajce.provider.keystore.BC$Mappings</pattern>
    <pattern>com.android.org.bouncycastle.jcajce.provider.keystore.bc.BcKeyStoreSpi</pattern>
    <pattern>com.android.org.bouncycastle.jcajce.provider.keystore.bc.BcKeyStoreSpi$Std</pattern>
    <pattern>com.android.org.bouncycastle.jce.provider.PKIXCertPathValidatorSpi</pattern>
    <!-- Use AndroidDigestFactoryOpenSSL as AndroidDigestFactory implementation -->
    <pattern>com.android.org.bouncycastle.crypto.digests.AndroidDigestFactoryOpenSSL</pattern>
  </forceLinkClasses>

If you want more help on this one you should come up with a minimal test case and create a new issue and refer to the test app: https://github.com/robovm/robovm/issues/new 

Reply all
Reply to author
Forward
0 new messages