gpt4 book ai didi

java - 同时发出 JSON 请求

转载 作者:行者123 更新时间:2023-11-29 23:12:39 26 4
gpt4 key购买 nike

我想从多个库存 JSON 数据中获取数据,但我知道如何一次只获取一个。我找不到办法得到几个。如果我只请求 AAPPL,它会工作,但如果我也请求 FB,它就不会工作

有关我从中获取数据的 API 的更多信息: https://financialmodelingprep.com/developer/docs#Realtime-Stock-Price

我尝试添加更多股票最终字符串 stockID = "AAPL,FB";

在浏览器中显示数据 https://financialmodelingprep.com/api/company/price/AAPL,FB?datatype=json但不在应用程序中。

public class MainActivity extends AppCompatActivity {
ListView textView;
ArrayList<String> stocks;//is a resizable array
ArrayAdapter<String> adapter; //Returns a view for each object in a collection of data objects you provide
RequestQueue queue; //Cola peticiones volley

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
queue = Volley.newRequestQueue(this); //Creamos una requestqueue para que gestione hilos, peticiones y demas.
stocks = new ArrayList<>();
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, stocks); //Pasing the context, the row layout and the resource?
textView.setAdapter(adapter); //Setting to the listview the arrayadapter that returns the view from the arraylist
addstock();

}
//TODO: add the rest of the stocks
private void addstock() {
final String stockID = "AAPL";
final String stockName = "Apple";






String url = "https://financialmodelingprep.com/api/company/price/" +stockID+"?datatype=json";

//Making the request
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
try{

JSONObject value = response.getJSONObject(stockID);

String price = value.getString("price");
String Linestock = stockName+ ":"+price+"$";
stocks.add(Linestock);//Adding it to the arraylist
adapter.notifyDataSetChanged(); //And to the view
} catch (Exception e){

}
}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
// TODO: Handle error

}

});
queue.add(jsonObjectRequest);
}
}

最佳答案

您可以使您的请求异步。为此,您可以查看此 tutorial :


我个人使用 okhttp3 包。有了它,您可以构建这样的函数:

public static String performPostCall(String requestURL, JSONObject postDataParams, String auth)
{
String response;

Log.d(LOG_TAG, "Send: " + postDataParams.toString() + " to " + requestURL + " Auth: " + auth);

try
{
Request request;

Builder tmp = new Builder();

tmp.connectTimeout(10, TimeUnit.SECONDS);
tmp.readTimeout(30, TimeUnit.SECONDS);
tmp.writeTimeout(30, TimeUnit.SECONDS);

OkHttpClient client = tmp.build();

MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, postDataParams.toString());

if (auth.equals(""))
{
request = new Request.Builder().url(requestURL).post(body).addHeader("Accept", "application/json").addHeader("Content-Type", "application/json").build();
}
else
{
request = new Request.Builder().url(requestURL).post(body).addHeader("Accept", "application/json").addHeader("Content-Type", "application/json").addHeader("authorization", "Bearer " + auth).build();
}

Response recv = client.newCall(request).execute();
response = recv.body().string();
}
catch (Exception e)
{
response = "{\"api_version\":1,\"status\":-1}";

Log.e(LOG_TAG, e.getMessage());
}

Log.d(LOG_TAG, "Received: " + response);

return response;
}

关于java - 同时发出 JSON 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55810320/

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