如何从单个asynctask执行多个URL?在我的应用程序中,我有四个URL。单个GET请求中的四个URL的Iwant数据。如何在android中使用AsyncTask
public class SOCreation extends AppCompatActivity {
private ArrayAdapter<String> adapter;
SalesOrganization salesOrganization = new SalesOrganization();
SalesDistribution salesDistribution = new SalesDistribution();
SoldToParty soldToParty = new SoldToParty();
Division division = new Division();
String[] ITEMS = {"1", "2", "3", "4", "5"};
MaterialSpinner spinner1,spinner2,spinner3,spinner4,spinner5;
RelativeLayout relativeLayout;
String qua="",mat="";
public static final String url1 = "http://192.168.1.5:8002/sap/opu/odata/sap/ZSALES_APP_SRV/SaleOrgSet?$format=json";
public static final String url2 = "http://192.168.1.5:8002/sap/opu/odata/sap/ZSALES_APP_SRV/DistrChannelSet?$filter=SalesOrg%20eq%20%271020%27&$format=json";
public static final String url3 = "http://192.168.1.5:8002/sap/opu/odata/sap/ZSALES_APP_SRV/DivsnSet?$filter=SalesOrg%20eq%20%271020%27%20and%20DistChannel%20eq%20%2720%27&$format=json";
public static final String url4 = "http://192.168.1.5:8002/sap/opu/odata/sap/ZSALES_APP_SRV/CustomersSet?$filter=SalesOrg%20eq%20%271020%27%20and%20DistrChannel%20eq%20%2720%27%20and%20Division%20eq%20%2700%27&$format=json";
String authorizationString = "Basic " + Base64.encodeToString(("abaper" + ":" + "erp@786").getBytes(), Base64.NO_WRAP);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_so_creation);
new GetAsync().execute(url1,url2,url3,url4);
公共类GetAsync扩展了AsyncTask {
@Override
protected void onPreExecute() {
}
@Override
protected JSONObject[] doInBackground(String... urls) {
InputStream inputStream = null;
HttpURLConnection urlConnection = null;
String line = "", response = "";
JSONParser jParser = new JSONParser();
try {
URL url = new URL(urls[0]);
JSONObject json1 = jParser.getJSONFromUrl(urls[1]);
JSONObject json2 = jParser.getJSONFromUrl(urls[2]);
JSONObject json3 = jParser.getJSONFromUrl(urls[3]);
JSONObject json4 = jParser.getJSONFromUrl(urls[4]);
URL urlToken = new URL("http://192.168.1.5:8002/sap/opu/odata/sap/ZSALES_APP_SRV/");
String token = new TokenGeneration().getToken(authorizationString, urlToken);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Authorization", authorizationString);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestProperty("X-CSRF-Token", token);
urlConnection.setRequestMethod("GET");
答案 0 :(得分:1)
使用排球。它一次支持多个请求。
<强> 1。为每个Web服务创建4个布尔变量。 2.如果您从Web服务1获得任何响应,则存储到全局变量并将布尔值1设置为true,依此类推4个请求。 3.当网络服务完成后,将工作流程发送到一个公共函数,在那里你可以检查布尔变量。如果所有变量都等于真,那么就按照你的反应做你想做的事。
答案 1 :(得分:0)
在从SAP Gateway获取数据时,使用$ batch方法在单个请求中对4个请求进行分组。代码示例和文档可在https://help.sap.com/saphelp_nw74/helpdata/en/94/a126519eff236ee10000000a445394/content.htm
获得