繁体   English   中英

用字符串实例化后,JSONObject返回一个非null值“ null”

[英]JSONObject returns a non null value of “null” after instantiating with string

我需要下载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);
    }

第一个断言通过,第二个未通过,导致length == 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.+'
}

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

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

testCompile 'org.json:json:the_correct_version'

在这里查看版本:

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

这是基于此帖子的答案: 未嘲笑Android单元测试

首先将json字符串转换为JSONArray,然后使用该JSONArray对象获取每个JSONObject

请尝试一下

String json = buffer.toString();
JSONObject jsonObject = new JSONObject(json);
JsonObject updatedJson = jsonObject.optJSONObject("json");
return updatedJson;

亲爱的您首先尝试仅获取json = buffer.toString(); 告诉我json的值。

如果您的字符串json为null,则文件读取代码有问题。

暂无
暂无

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

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