我是Android新手,自從我上次編碼以來已經很長時間了。對不起,如果這是一個虛假的問題。當Android中的活動開始時,Json請求被處理了幾次。
我的應用程序要求用戶介紹將在API中搜索的索引單詞。用戶輸入然後用於創建一個URL來請求JSONObjects。這是在第一個活動中完成的。第二個活動接收URL並向API發出請求。
我正在使用第三方庫在Tinder-like刷卡效果視圖(https://github.com/Diolor/Swipecards)中顯示結果。該庫有一個名爲onAdapterAboutToEmpty的方法,其中包含了JSONObjectRequest。我也使用Volley創建一個RequestQueue。
但是,此方法在創建第二個活動時被調用三次。我只需要調用一次。所以,我的問題如下:
- 這是由Activity生命週期引起的,還是可能是由第三方庫引起的問題?
- 這會影響應用程序的性能嗎?
這裏有代碼:
public class DisplayQueryActivity extends AppCompatActivity {
RequestQueue mRequestQueue;
ResearchAdapter mJSONAdapter;
SwipeFlingAdapterView mSwipePaper;
String user_urlQuery;
public int index_Springer_Query;
public int number_papers_deleted = 0;
static final int PICK_USER_ACTION = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_query);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//Get data from previous Activity
Intent intentDisplayQuery = getIntent();
user_urlQuery = intentDisplayQuery.getStringExtra(UserInput.EXTRA_QUERY);
index_Springer_Query = extractFieldQuery(user_urlQuery, URL_RESULT_STARTING);
mJSONAdapter = new ResearchAdapter(this, getLayoutInflater(), getProviderLogoImage(user_urlQuery));
mRequestQueue = Volley.newRequestQueue(this);
mSwipePaper = (SwipeFlingAdapterView) findViewById(R.id.swipe_content);
}
@Override
protected void onStart(){
super.onStart();
mSwipePaper.setAdapter(mJSONAdapter);
displaySwipeMode();
}
private void displaySwipeMode(){
mSwipePaper.setFlingListener(new SwipeFlingAdapterView.onFlingListener() {
@Override
public void removeFirstObjectInAdapter() {
mJSONAdapter.remove(0);
number_papers_deleted++;
mJSONAdapter.notifyDataSetChanged();
}
@Override
public void onLeftCardExit(Object dataObject) {
Toast.makeText(DisplayQueryActivity.this, "Left!", Toast.LENGTH_SHORT).show();
}
@Override
public void onRightCardExit(Object dataObject) {
Toast.makeText(DisplayQueryActivity.this, "Right!", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdapterAboutToEmpty(int itemsInAdapter) {
sendJsonRequest();
}
@Override
public void onScroll(float v) {
}
});
}
private void sendJsonRequest() {
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET,
user_urlQuery,
(String) null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
mJSONAdapter.updateData(response.optJSONArray(VR_RECORDS));
Log.d("Console says: ", response.toString());
}
},
new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError error){
Log.d("Console says: ", error.getMessage().toString());
}
});
mRequestQueue.add(request);
}
任何幫助將不勝感激。謝謝。
我想我的第一個問題是,你爲什麼要嘗試發送這種方法的json請求?看起來像一個奇怪的地方做到這一點。您顯然希望在非常特定的時間發送json請求,並滿足確切的條件。所以一個名爲「somethingABOUTtoppen」的方法似乎不適合這樣做。 – NoChinDeluxe
感謝您的回答@NoChinDeluxe。首先,我試圖在這個方法中發送json請求,因爲當適配器有5個元素時會自動調用這個請求。因此,當用戶向右或向左滑動時,適配器必須用先前未請求的新元素重新填充。我在這裏稱它是因爲它對我來說看起來很合邏輯。 – user274051
其次,在開始時我只調用了onCreate和onAdapterAboutToEmpty方法。但是,當我測試它時,我意識到當活動創建時,該方法是自稱的。因此,我從onCreate中消除了兩次不請求的請求。我還沒有創建這些方法。他們來自第三方圖書館。 你會發展的方式,你會這樣做嗎?我非常樂於接受建議。正如我所說,我是新來的。再次感謝 – user274051