【问题标题】:Getting error when using HttpUrlConnection with Android compiling against SDK 21将 HttpUrlConnection 与针对 SDK 21 的 Android 编译一起使用时出现错误
【发布时间】:2014-12-01 11:29:45
【问题描述】:

我找到了问题所在,它根本不在我发布的代码中。多线程的危险以及最近才切换到 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 子句回调中抛出空指针异常。next 给出 NullPtrException。

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

我不知道出了什么问题。

【问题讨论】:

    标签: java android httpurlconnection


    【解决方案1】:

    我认为你不能在主线程上运行它——你需要用 AsyncTask 来包装它。

    【讨论】:

      猜你喜欢
      • 2019-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-10
      • 2017-07-08
      • 1970-01-01
      • 2012-10-08
      相关资源
      最近更新 更多