繁体   English   中英

将HttpUrlConnection与针对SDK 21的Android编译时出现错误

[英]Getting error when using HttpUrlConnection with Android compiling against SDK 21

我找到了问题,根本不在我发布的代码中。 mulTi线程的风险,以及最近切换到Android Studio的风险。 SDK 21中的菜单存在问题,并且我没有使用指南来切换为使用Android 5.0主题和ActionBar。

我在使用HttpUrlConnection时遇到问题。 我用它来调用用PHP编写的api。 该应用程序下载有关启动的文章摘要,并且效果很好。 当您单击某篇文章时,它将下载该文章的其余信息,并在新活动中显示该信息。 至少应该认为它现在已经可以在您单击摘要时开始崩溃。 在以下代码中到达conn.connect行时崩溃

public String downloadUrl(String myurl) throws IOException {
    InputStream is = null;

    HttpURLConnection conn=null;
    String contentAsString=null;
    try {
        URL url = new URL(myurl);
        conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("GET");
        conn.setInstanceFollowRedirects(true);
        conn.setDoInput(true);

        // Starts the query
        conn.connect();
        int response = conn.getResponseCode();
        Log.d("HttpReader", "The response is: " + response);
        is = conn.getInputStream();

        // Convert the InputStream into a string
        contentAsString = readIt(is);



    // Makes sure that the InputStream is closed after the app is
    // finished using it.


    } finally {
        conn.disconnect();
        if (is != null) {
            is.close();
        }
    }
    return contentAsString;
}

我已将错误跟踪到文件Choreographer.java和以下函数

  void doCallbacks(int callbackType, long frameTimeNanos) {
    CallbackRecord callbacks;
    synchronized (mLock) {
        // We use "now" to determine when callbacks become due because it's possible
        // for earlier processing phases in a frame to post callbacks that should run
        // in a following phase, such as an input event that causes an animation to start.
        final long now = SystemClock.uptimeMillis();
        callbacks = mCallbackQueues[callbackType].extractDueCallbacksLocked(now);
        if (callbacks == null) {
            return;
        }
        mCallbacksRunning = true;
    }
    try {
        for (CallbackRecord c = callbacks; c != null; c = c.next) {
            if (DEBUG) {
                Log.d(TAG, "RunCallback: type=" + callbackType
                        + ", action=" + c.action + ", token=" + c.token
                        + ", latencyMillis=" + (SystemClock.uptimeMillis() - c.dueTime));
            }
            c.run(frameTimeNanos);
        }
    } finally {
        synchronized (mLock) {
            mCallbacksRunning = false;
            do {
                final CallbackRecord next = callbacks.next;
                recycleCallbackLocked(callbacks);
                callbacks = next;
            } while (callbacks != null);
        }
    }
}

在finally子句callbacks中将引发空指针异常。next提供一个NullPtrException。

我已经测试了我在Chrome中使用的url,它重新运行了应该使用的JSON,并且还使用curl进行了测试,再次得到了我期望的JSON。

我不确定是怎么了。

我不认为您可以在主线程上运行它-您需要使用AsyncTask对其进行包装。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM