在使用string实例化后,JSONObject返回非null值"null"

LLL*_*LLL 7 java android jsonobject

我需要下载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;
}
}
Run Code Online (Sandbox Code Playgroud)

测试方法

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

第一个断言传递,第二个不传递,导致长度== 0.

而我得到的就是这个.值为字符串"null"的JSONObject对象.

没有异常被抛出.我将缓冲区的内容写入文件并验证它,并且验证正常.

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

为什么会这样?

摇篮

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.+'
}
Run Code Online (Sandbox Code Playgroud)

Axe*_*elH 8

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

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

testCompile 'org.json:json:the_correct_version'
Run Code Online (Sandbox Code Playgroud)

看到这里的版本:

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

这是基于这篇文章的答案:Android单元测试没有嘲笑