簡體   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