【问题标题】:JSONObject returns a non null value of "null" after instantiating with stringJSONObject 在用字符串实例化后返回一个非空值“null”
【发布时间】:2016-09-20 05:24:53
【问题描述】:

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

为什么会这样?

分级

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.+'
}

【问题讨论】:

  • 我认为您在添加该对象的值后需要一个 断点。尝试在return 之前添加一个虚拟行,如Log.i("tag", "DUMMY"); 并在其上放置断点。
  • 这里JSONObject 类属于哪个库??是google.simple.json 还是apatche ??哪一个这真的让我们很困惑,因为你没有提到导入语句。@LLL
  • BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); 告诉它使用 unicode 解码器。
  • 由于您在单元测试中运行它,这没有使用任何设备上的库,SDK 不提供代码,只是一个签名,我猜是返回“null”;)尝试将 jar 包含在 gradle 中以查看结果。

标签: java android jsonobject


【解决方案1】:

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

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

testCompile 'org.json:json:the_correct_version'

在这里查看版本:

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

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

【讨论】:

  • 当前版本为:20160212
  • 在 OP 的 lala 土地上,一切都很幸福...... :D
  • 从此 OP 过上了幸福的生活。鳍。
  • Android 的正确版本是:org.json:json:20080701,否则getString() 中的null 会有不同的行为。在 Android 上它会返回 null,在早期版本上它会抛出一个 Exception
  • 你能给我一些关于 2008 年后版本例外的文档吗?
【解决方案2】:

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

【讨论】:

  • 如果我执行 new JSONArray(json),JSONArray 也是“null”
【解决方案3】:

请尝试一下

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

【讨论】:

  • 我试过他的代码,我得到了jsonObject中的json值
【解决方案4】:

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

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-02
  • 1970-01-01
  • 2016-03-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多