JAVA使用JSON检索当前比特币价格
我想通过使用JSON获取当前/历史比特币价格 但是,代码显示了以下错误JAVA使用JSON检索当前比特币价格,java,json,bitcoin,Java,Json,Bitcoin,我想通过使用JSON获取当前/历史比特币价格 但是,代码显示了以下错误 Exception in thread "main" java.lang.NullPointerException at RwithJlab.Basic.main(Basic.java:19) ---------------------------------代码--------------------------------- package RwithJlab; import java.io.BufferedRead
Exception in thread "main" java.lang.NullPointerException at RwithJlab.Basic.main(Basic.java:19)
---------------------------------代码---------------------------------
package RwithJlab;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Basic
{
public static void main(String[] args) throws MalformedURLException, IOException, JSONException
{
JSONObject data = getJSONfromURL("https://blockchain.info/charts/market-price?format=json");
JSONArray data_array = data.getJSONArray("values");
for (int i = 0; i < ((CharSequence) data_array).length(); i++)
{
JSONObject price_point = (JSONObject) data_array.get(i);
// Unix time
int x = price_point.getInt("1364062505");
// Bitcoin price at that time
double y = price_point.getDouble("y");
// Do something with x and y.
System.out.println(x);
}
}
public static JSONObject getJSONfromURL(String URL) throws JSONException
{
try
{
URLConnection uc;
URL url = new URL(URL);
uc = url.openConnection();
uc.setConnectTimeout(10000);
uc.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
uc.connect();
BufferedReader rd = new BufferedReader(
new InputStreamReader(uc.getInputStream(),
Charset.forName("UTF-8")));
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1)
{
sb.append((char)cp);
}
String jsonText = (sb.toString());
return new JSONObject(jsonText.toString());
} catch (IOException ex)
{
return null;
}
}
}
包RwithJlab;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.net.URLConnection;
导入java.nio.charset.charset;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
公共课基础
{
publicstaticvoidmain(String[]args)抛出畸形的durlexception、IOException、JSONException
{
JSONObject数据=getJSONfromURL(“https://blockchain.info/charts/market-price?format=json");
JSONArray data_array=data.getJSONArray(“值”);
对于(int i=0;i<((字符序列)数据_数组).length();i++)
{
JSONObject price_point=(JSONObject)data_array.get(i);
//Unix时间
int x=价格_point.getInt(“1364062505”);
//比特币当时的价格
双y=价格_点。getDouble(“y”);
//用x和y做点什么。
系统输出println(x);
}
}
公共静态JSONObject getJSONfromURL(字符串URL)抛出JSONException
{
尝试
{
urlc;
URL=新URL(URL);
uc=url.openConnection();
uc.设置连接超时(10000);
addRequestProperty(“用户代理”、“Mozilla/4.0(兼容;MSIE6.0;WindowsNT5.0)”);
uc.connect();
BufferedReader rd=新的BufferedReader(
新的InputStreamReader(uc.getInputStream(),
字符集forName(“UTF-8”);
StringBuilder sb=新的StringBuilder();
int-cp;
而((cp=rd.read())!=-1)
{
sb.追加((char)cp);
}
字符串jsonText=(sb.toString());
返回新的JSONObject(jsonText.toString());
}捕获(IOEX异常)
{
返回null;
}
}
}
请帮助捕获异常时,您至少应该记录异常,否则在异常发生时您就不知所措了 在本例中,您正在捕获一个
IOException
并返回null
。这将导致以后出现NullPointerException
,但您无法看到根本原因
记录
IOException
(至少调用ex.printStackTrace()
),您将看到真正的原因。您在int x=price_point.getInt(“1364062505”)行获得jsoneexception
代码>
首先,查看您试图从中检索的源JSON
它的结构是:
values: [ {x : timestamp, y : value}, ... ]
其中,时间戳是以毫秒表示的日期,值是以美元表示的BTC价格
您正在尝试getInt(“1364062505”)
,而值中的此类键不存在
如果找不到键或无法将值转换为整数(),则在getInt(key)
处抛出JSONException
您需要编写intx=price\u point.getInt(“x”)
,或者更好—将getInt()
替换为optInt()
,将getDouble()
替换为optDouble()
。在这种情况下,如果JSONObject
中不存在这样的键,则不会得到任何异常,只有零。然后您应该检查中的值,如果(value!=0)
块您应该看到NullPointerException之前的堆栈跟踪。