gpt4 book ai didi

java - 在 Java 中检索数据

转载 作者:行者123 更新时间:2023-11-30 07:18:49 25 4
gpt4 key购买 nike

我是一个java新手。是否可以从网站获取数据然后将其存储在某种数据结构中?例如,该程序在给定时间从雅虎财经获取股票的值(value)并存储它。就像我说的,我不太精通 Java,我想知道这是否可以完成。如果可以,做起来是不是很难?

最佳答案

    public class GetYahooData
{
public ArrayList<JSONObject> getOutputFromUrl(String url)
{
ArrayList<JSONObject> output = new ArrayList<JSONObject>();
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse response;
StringBuilder builder= new StringBuilder();
JSONObject myjson ;
JSONArray the_json_array;
try
{
response = httpClient.execute(httpPost);
BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
char[] buf = new char[8000];
int l = 0;
while (l >= 0)
{
builder.append(buf, 0, l);
l = in.read(buf);
}
myjson = new JSONObject("{child:"+builder.toString()+"}");
JSONObject mmm = new JSONObject(builder.toString());
JSONArray mmmArr = mmm.getJSONArray("status");
the_json_array = myjson.getJSONArray("child");
for (int i = 0; i < the_json_array.length(); i++)
{
JSONObject another_json_object = the_json_array.getJSONObject(i);//the_json_array.getJSONObject(i);
output.add(another_json_object);
}
} catch (ClientProtocolException e) {
System.out.println("ClientProtocolException :"+e);
e.printStackTrace();
} catch (IOException e) {
System.out.println("IOException :"+e);
e.printStackTrace();
} catch (JSONException e) {
System.out.println("JSONException hussain :"+e);
e.printStackTrace();
}
return output;
}
}

public class useYahoo
{
public static void main(String args[])
{
String url = "the url you want the response from";
getYahooData object = new GetYahooData();
ArrayList<JSONObject> output = object.getOutputFromUrl(url);
}
}

关于java - 在 Java 中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15103944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com