简体   繁体   English

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

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

I have located the problem, it is not in the code I posted at all. 我找到了问题,根本不在我发布的代码中。 The perils of mulTi-threading and and just recently switching to Android Studio. mulTi线程的风险,以及最近切换到Android Studio的风险。 It is a problem with the Menu in SDK 21 and my not using the guide to switch to using Android 5.0 Themes and ActionBar. SDK 21中的菜单存在问题,并且我没有使用指南来切换为使用Android 5.0主题和ActionBar。

I am having problems with using HttpUrlConnection. 我在使用HttpUrlConnection时遇到问题。 I am using it to call an api written in PHP. 我用它来调用用PHP编写的api。 The app downloads summaries of articles on startup and that works fine. 该应用程序下载有关启动的文章摘要,并且效果很好。 When you click on an article it then download the rest of the information for the article and displays the info in a new activity. 当您单击某篇文章时,它将下载该文章的其余信息,并在新活动中显示该信息。 At least it is supposed to it has now started crashing anytime you click on a summary. 至少应该认为它现在已经可以在您单击摘要时开始崩溃。 It crashes when it reaches the line conn.connect in the following code 在以下代码中到达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;
}

I have traced the error to the file Choreographer.java and the following function 我已将错误跟踪到文件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);
        }
    }
}

A null pointer exception is thrown in the finally clause callbacks.next gives an NullPtrException. 在finally子句callbacks中将引发空指针异常。next提供一个NullPtrException。

I have tested the url I am using in Chrome and it retruns the JSON it is supposed to and also tested with curl and again I get the JSON I expect. 我已经测试了我在Chrome中使用的url,它重新运行了应该使用的JSON,并且还使用curl进行了测试,再次得到了我期望的JSON。

I am not sure what is wrong. 我不确定是怎么了。

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

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

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