나는 연대순으로 전화 기능에 노력하고 있지만, 기능은 parseJSONTwo()
기능을하기 전에 실행하는 것 parseJSON()
(두 기능은 단지 URL에서 JSON 데이터를 가져 오기)
package com.example.rechev;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class pratim extends AppCompatActivity {
RequestQueue queue;
TextView degem;
String model;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pratim);
degem = findViewById(R.id.pratim);
queue = Volley.newRequestQueue(this);
parseJSON();
parseJSONTwo();
}
public void parseJSON() {
Intent intent = getIntent();
int number = intent.getIntExtra("inumber", 1234567);
String url = "http://apiurl.com" + number}";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject result = response.getJSONObject("res");
JSONArray jsonArray = result.getJSONArray("rec");
String test = result.getString("total");
if (Integer.parseInt(test) == 1) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject number = jsonArray.getJSONObject(i);
String carMaker = number.getString("toze");
degem.append(String.valueOf("\n maj: " + carMaker));
model = number.getString("degem_nm");
if (carGimur.length() >= 1) {
degem.append(String.valueOf("\n type: " + carGimur));
}
}
} else {
degem.append("invalid");
}
} catch (JSONException e) {
degem.append(e.toString());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
degem.append("Error");
}
});
queue.add(request);
}
public void parseJSONTwo() {
String url = "http://apiurl.com"+model}";
JsonObjectRequest requestt = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject result = response.getJSONObject("res");
JSONArray jsonArray = result.getJSONArray("rec");
String test = result.getString("total");
if (Integer.parseInt(test) == 1) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject number = jsonArray.getJSONObject(i);
String carNefah = number.getString("nefah_manoa");
degem.append(String.valueOf("num: " + carNefah));
}
}
} catch (JSONException e) {
degem.append(e.toString());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
degem.append("Error");
}
});
queue.add(requestt);
}
}
함수 parseJSON()
JSON의 데이터 페치 url
(A) 내로 및 풋을 TextView
, 또한 글로벌 변수 (모델) 값, 설정 parseJSONTWO()
은 제에 기초하여 데이터를 페치 할 수 있도록 그 URL에 사용하는 parseJSON()
기능.
문제가되는 parseJSONTwo()
이 정보 사용할 수 있도록, 어떤 이유로 먼저 호출되고 있습니다 parseJSON()
를 검색합니다.
업데이트 된 코드 :
package com.example.rechev;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class pratim extends AppCompatActivity {
RequestQueue queue;
TextView degem;
String model;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pratim);
degem = findViewById(R.id.pratim);
queue = Volley.newRequestQueue(this);
parseJSON();
}
public void parseJSON() {
Intent intent = getIntent();
int number = intent.getIntExtra("inumber", 1234567);
String url = "http://apiurl.com" + number}";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject result = response.getJSONObject("res");
JSONArray jsonArray = result.getJSONArray("rec");
String test = result.getString("total");
if (Integer.parseInt(test) == 1) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject number = jsonArray.getJSONObject(i);
String carMaker = number.getString("toze");
degem.append(String.valueOf("\n maj: " + carMaker));
model = number.getString("degem_nm");
if (carGimur.length() >= 1) {
degem.append(String.valueOf("\n type: " + carGimur));
}
}
} else {
degem.append("invalid");
}
if(model.length() >= 1){
parseJSONTwo();
}
} catch (JSONException e) {
degem.append(e.toString());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
degem.append("Error");
}
});
queue.add(request);
}
public void parseJSONTwo() {
String url = "http://apiurl.com"+model}";
JsonObjectRequest requestt = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject result = response.getJSONObject("res");
JSONArray jsonArray = result.getJSONArray("rec");
String test = result.getString("total");
if (Integer.parseInt(test) == 1) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject number = jsonArray.getJSONObject(i);
String carNefah = number.getString("nefah_manoa");
degem.append(String.valueOf("num: " + carNefah));
}
}
} catch (JSONException e) {
degem.append(e.toString());
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
degem.append("Error");
}
});
queue.add(requestt);
}
}
이제 parseJSONTwo
올바른 URL을 얻을 것이다하지만 기능이 완료되지 않습니다.
이러한 기능의 각각에서 parseJSON()
그리고 parseJSONTWO()
, 당신은 비동기 네트워크 요청을하고 있습니다. 네트워크 요청은 훨씬 느린 프로그램이 실행보다 밀리 초 수십 밀리 초에서 어디를 복용 즉시 없습니다. 네트워크 요청이 해당 해결하기 위해 약간의 시간이 걸릴 때문에 onResponse()
기능을 관련 요청이 돌아올 때 또한 지연 및 화재 있습니다.
따라서, 코드에서 작업의 순서는
- 통화
parseJSON()
시간이 걸릴 수 있도록 GET 요청을 시작합니다 - 전화
parseJSONTwo()
의 GET 요청의 응답을 기다리지 않고 다른 GET 요청을 시작parseJSON()
하고 그은을뿐만 아니라 동안 걸릴 수 있습니다
함수는 parseJSONTwo()
함수 직후라고 parseJSON()
GET 요청 시간이 걸릴 수 있기 때문에 아직 완료되지 않을 수 있습니다 GET 요청을 시작한 후 돌아갑니다. 의 GET 요청은 그래서 parseJSON()
여전히 응답 과정에서 그리고없이 할 수있는 기능이 때 parseJSONTwo()
다음 GET 요청을 시작하기 위해 호출됩니다.
요청 반환 각각은 그 때 onResponse()
콜백이 실행됩니다. 그들이 반납 그러나 순서는 지정하지 않는다. 그것은 서버와 네트워크 상태 및 시간을 각각 요청이 완료 될 때까지 소요에 따라 달라집니다.
기능이 있기 때문에 그리고 parseJSONTwo()
함수의 응답을 필요로 parseJSON()
두 번째 기능 것을, 그래서 당신은 체인에 이러한 기능을 가지고 parseJSONTwo()
첫 번째 함수의 GET 요청의 응답이 수신 된 경우에만 호출된다.
체인 이러한 기능을하는 한 가지 방법은 함수를 호출하는 parseJSONTwo()
의 OnResponse()
함수 핸들러 parseJSON()
.