libgdx iOS httpUrlConnection.connect 抛出 IllegalMonitorStateException

libgdx iOS httpUrlConnection.connect throw IllegalMonitorStateException

我有一款 Libgdx 游戏,在 android 上 运行 没问题,但是当我在 iOS 上 运行 时(libgdx 1.5 .2 和 robovm 1.0.0-beta-2) 我在尝试从 facebook 下载头像图像时遇到问题。我使用 facebook api.

请求 URL

代码(在下面提供)在 "syncDownloadPixmapFromURL()" 函数中的 "conn.connect()" 上崩溃。 我做错了什么?

编辑:可能是某个地方的 HttpConnection 已经存在并且某些东西(例如某些 fb 请求?)在 运行在新线程中尝试从另一个线程使用它?

我收到以下错误消息:

2015-01-13 19:27:14.949 IOSLauncher[531:112238] [info] imagemanager: connecting
java.lang.IllegalMonitorStateException: unlock of unowned monitor
java.lang.IllegalMonitorStateException: unlock of unowned monitor
at com.android.okhttp.Connection.upgradeToTls(Connection.java)
at com.android.okhttp.Connection.connect(Connection.java)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java)
at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java)
at com.android.okhttp.internal.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java)
at bla.helpers.ImageManager.syncDownloadPixmapFromURL(ImageManager.java)
at bla.helpers.ImageManager.access(ImageManager.java)
at bla.helpers.ImageManager.run(ImageManager.java)
at java.lang.Thread.run(Thread.java)
at com.android.okhttp.Connection.upgradeToTls(Connection.java)
at com.android.okhttp.Connection.connect(Connection.java)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java)
at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java)
at com.android.okhttp.internal.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java)
at bla.helpers.ImageManager.syncDownloadPixmapFromURL(ImageManager.java)
at bla.helpers.ImageManager.access(ImageManager.java)
at bla.helpers.ImageManager.run(ImageManager.java)
at java.lang.Thread.run(Thread.java)
[WARN] android.System: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
[WARN] android.System: java.lang.Throwable: Explicit termination method 'close' not called
at dalvik.system.CloseGuard.open(CloseGuard.java)
at com.android.org.conscrypt.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java)
at com.android.okhttp.Connection.upgradeToTls(Connection.java)
at com.android.okhttp.Connection.connect(Connection.java)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java)
at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java)
at com.android.okhttp.internal.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java)
at bla.helpers.ImageManager.syncDownloadPixmapFromURL(ImageManager.java)
at bla.helpers.ImageManager.access(ImageManager.java)
at bla.helpers.ImageManager.run(ImageManager.java)
at java.lang.Thread.run(Thread.java)
Cannot detach thread when there are non native frames on the call stack

代码:

protected void  asyncDownloadTextureFromURL(final URL url, final FileHandle image, final boolean round) 
{           
    if(beingDownloaded.contains(url.toString())) 
        return;         
    if(threadAmount>=MAX_THREAD_AMOUNT) 
        return; 
    threadAmount++;
    new Thread(new Runnable() 
    {               
        @Override
        public void run() 
        {                   
            beingDownloaded.add(url.toString());

            Gdx.app.log("imagemanager", "Downloading started: "+url.toString());
            Pixmap pixmap = syncDownloadPixmapFromURL(url);
            if(image.exists()) image.delete();
            if(pixmap==null)
            { 
                beingDownloaded.remove(url.toString());
                threadAmount--;
                return;
            }
            Pixmap to_save = null;
            OutputStream stream = image.write(false);
            try 
            {
        /**
        *TO GET PNG ENCODER CLASS FOLLOW TO: http://pastebin.com/psF60D8Q
        **/
                to_save = pixmap;
                if(round)
                {
                    to_save = PNG.RoundPixmap(pixmap);
                    pixmap.dispose();
                }
                byte[] bytes = PNG.toPNG(to_save);

                stream.write(bytes);
                stream.close();
                Gdx.app.log("imagemanager", "Downloading stopped: "+url.toString());

                if(!downloaded.contains(url.toString()))
                {
                    downloaded.add(url.toString());
                    long downloaded_date = new Date().getTime();
                    download_dates.add(downloaded_date);
                }
                beingDownloaded.remove(url.toString());

                if(image.exists())
                    Gdx.app.log("imagemanager","saved file: " + image.toString());
                else
                    Gdx.app.log("imagemanager", "error saving file: " + image.toString());
            } 
            catch (IOException e) 
            {
                Gdx.app.log("imagemanager", "error saving file: " + image.toString());
            }
            finally
            {
                if(to_save != null)
                    to_save.dispose();
                threadAmount--;
            }
        }
    }).start();
}   

private Pixmap syncDownloadPixmapFromURL(URL url) 
{   
    Gdx.app.log("imagemanager", "sync Downloading started");

    try 
    {

        Gdx.app.log("imagemanager", "create connection");


        HttpURLConnection conn= (HttpURLConnection)url.openConnection();

        conn.setDoInput(true);
        Gdx.app.log("imagemanager", "connecting");
        conn.connect(); 
        int length = conn.getContentLength();
        if(length<=0) 
            return null;
        Gdx.app.log("imagemanager", "get input stream");
        InputStream is = conn.getInputStream();
        DataInputStream dis = new DataInputStream(is);
        byte[] data = new byte[length];
        Gdx.app.log("imagemanager", "read fully");
        dis.readFully(data);
        Gdx.app.log("imagemanager", "Create pixmap");

        Pixmap pixmap = new Pixmap(data, 0, data.length);
        pixmap = ImagePow2.isPow2(pixmap)?pixmap:ImagePow2.getPow2Pixmap(pixmap);
        return pixmap;
    } 
    catch (MalformedURLException e) 
    {   
        Gdx.app.log("imagemanager", "Error in sync download: wrong url");
    } 
    catch (IOException e) 
    {   
        Gdx.app.log("imagemanager", "Error in sync download: io exception");
    }
    return null;
 } 

此问题是由 robovm 插件的损坏安装引起的。重新安装解决了问题