[英]Volley in Android not attempting to connect
我一直試圖讓 Volley 從 inte.net 請求數據。 代碼如下。 請原諒冗長的代碼,這是一個快速而骯臟的測試,只是為了看看 Volley 是否有效。 一旦我開始工作,我會清理它。
public static Option parseJSON(Context context, String stockTicker, String strikePrice, String expiration) {
final String apikey = "XXXX"; **//key removed just for StackOverflow, but it works**
String ticker = "&symbol=";
final String baseURL = "https://api.tdameritrade.com/v1/marketdata/quotes?";
Option option = new Option();
try {
SimpleDateFormat simpleDate = new SimpleDateFormat("MM/dd/yyyy");
SimpleDateFormat simpleDateforRequest = new SimpleDateFormat("MMddyy");
String formattedDate = simpleDateforRequest.format(simpleDate.parse(expiration));
String fullTicker = stockTicker + "_" + formattedDate + "C" + strikePrice;
ticker = ticker + fullTicker;
} catch (ParseException e) {
e.printStackTrace();
}
final String url = baseURL + apikey + ticker;
RequestQueue queue = Volley.newRequestQueue(context);
JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
String jsonObject = response.toString();
Log.d("JSONRequestResponse", "Response: " + jsonObject);
}
}, error -> Log.d("JSONRequestResponse", "Error: " + error.toString()));
queue.add(request);
Log.d("JSON", "Request" + request + " URL: " + url);
return option; **//method doesn't create an option yet, but the problem comes well before this.**
}
問題是 JsonArrayRequest 中的 logd 都沒有被觸發,最后的只是一個空數組(“[]”),讓我覺得 Volley 沒有嘗試連接。
同樣,我只是在測試 Volley 是否檢索到 JSON,我還沒有得到返回值 Object。 在這一點上,我不知道它會是什么。 任何幫助深表感謝
更新****************************************
正如預期的那樣,應用程序似乎沒有連接到 inte.net。 我使用以下方法查看是否存在連接並返回 false:
public static boolean isNetworkAvailable() {
Log.d("CheckPoint", "isNetworkAvailable first line");
final boolean[] availability = {false};
new Thread(new Runnable() {
@Override
public void run() {
try {
availability[0] = InetAddress.getByName("www.google.com").isReachable(5000);
} catch (UnknownHostException e) {
Log.d("CheckPoint", "isNetworkAvailable UnknownHost!");
} catch (IOException e) {
Log.d("CheckPoint", "isNetworkAvailable IOException!");
}
}
}).start();
Log.d("CheckPoint", "Availability: " + availability[0]);
return availability[0];
}
你從來沒有打電話
requestQueue.start()
雖然我不得不說——除非你真的需要一些只有 Volley 給你的功能,否則 Retrofit 是一個更容易使用的庫,它避免了 Volley 的一些問題(Volley 可能導致 memory 大量響應或多個未完成請求的問題,因為它需要將整個響應保存為 memory 字符串,而不是將其流式傳輸到臨時文件)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.