JSONObject返回一个非null值"null".用字符串实例化之后 [英] JSONObject returns a non null value of "null" after instantiating with string

查看:316
本文介绍了JSONObject返回一个非null值"null".用字符串实例化之后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to download JSON and then store it in JSONObject.

我正在使用org.json.JSONArray.

I am using org.json.JSONArray.

所有代码都放在一个地方:

Here's all the code in one place:

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.

First assert passes, the second doesn't, cause length == 0.

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

And what I get is this. A JSONObject object with value of string "null".

没有引发异常.我将缓冲区的内容写到文件中并进行了验证,它可以很好地验证.

No exception is thrown. I wrote the contents of buffer to file and validated it, and it validates fine.

另一张图片 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不能提供所有功能,有些只是一些框架类,提供方法和文档的签名,但不提供代码.所以你不能直接执行.

Since you are running your code with Junit, it running on the SDK on your computer. This SDK doesn't provide everything, some are just some skeleton class providing signature of method and documentation but not the code. So you can't execute directly.

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

You need to import the library to be able to run it on testing.

testCompile 'org.json:json:the_correct_version'

在此处查看版本:

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

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

This is based on the answer on this post : Android unit test not mocked

这篇关于JSONObject返回一个非null值"null".用字符串实例化之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆