java - JSONObject 在使用字符串实例化后返回非空值 "null"

标签 java android json

我需要下载 JSON,然后将其存储在 JSONObject 中。

我正在使用 org.json.JSONArray。

这里是一个地方的所有代码:

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Test
{
    public static JSONObject getJSON()
    {
    try
    {
        URL uri = new URL("http://events.makeable.dk/api/getEvents");
        HttpURLConnection urlConnection = (HttpURLConnection) uri.openConnection();
        if (urlConnection.getResponseCode() != HttpURLConnection.HTTP_OK)
        {
            return null;
        }

        InputStream inputStream = urlConnection.getInputStream();

        if (inputStream != null)
        {
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
            StringBuffer buffer = new StringBuffer();

            try
            {
                String line;
                while ((line = br.readLine()) != null)
                {
                    buffer.append(line);
                }
                br.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }



            String json = buffer.toString();

            JSONObject jObject = null;
            try {
                jObject = new JSONObject(json);
            }
            catch (JSONException e) {
                e.printStackTrace();

            }
            int i = 1;
            return jObject;
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    return null;
}
}

测试方法

 @Test
    public void addition_isCorrect() throws Exception
    {
        JSONObject json = Test.getJSON();
        assertNotNull(json);
        assertTrue(json.length()>0);
    }

第一个断言通过,第二个不通过,导致长度 == 0。

我得到的是 this .值为字符串“null”的 JSONObject 对象。

没有抛出异常。我将缓冲区的内容写入文件并对其进行了验证,并且验证正常。

另一张图片http://i.imgur.com/P03MiEZ.png

为什么会这样?

Gradle

apply plugin: 'com.android.application'

android {

testOptions {
    unitTests.returnDefaultValues = true
}
compileSdkVersion 23
buildToolsVersion "24.0.0 rc3"

defaultConfig {
    applicationId "baaa.myapplication"
    minSdkVersion 23
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.+'
}

最佳答案

由于您正在使用 Junit 运行您的代码,因此它会在您计算机上的 SDK 上运行。此 SDK 不提供所有内容,有些只是一些骨架类,提供方法和文档的签名,但不提供代码。所以不能直接执行。

您需要导入库才能在测试时运行它。

testCompile 'org.json:json:the_correct_version'

在这里查看版本:

http://mvnrepository.com/artifact/org.json/json

这是基于这篇文章的答案:Android unit test not mocked

关于java - JSONObject 在使用字符串实例化后返回非空值 "null",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37389154/

相关文章:

Android 会将快速单点触摸视为移动?

android - 如何在android中的android列表中获取youtube视频的视频缩略图

java - Jackson API - 使用简单的动态对象反序列化 JSON

javascript - 如何将我自己的数据添加到响应式美国各州 map 中?

android - 为现有 Web 应用程序创建应用程序访问 URL 的优雅方法是什么?

java - 使用 Java 使用 BouncyCaSTLe 生成 X509Certificate

java - SpringFramework jsp设置限制JAVA url

java - Spring JPA 没有正在进行的事务

JAVAFX : Tableview row selection behaviour

android - 新的 Android Geofence Api - 示例代码在位置时不发出警报/通知