进去for循环的android多个地点的天气状况

问题描述 投票:1回答:3

我开发我使用中,我想知道一堆的位置,我已经存储在ArrayList<LatLng>的天气状况黑暗的天空API天气应用程序。

我使用OKHttp解析从API的JSON数据,所以我里面试图循环全取过程循环,但它并没有得到期望的输出。

private void beginTask(ArrayList<LatLng> arrayLis) { 
    //arraylis contains list of locations(LatLng)
    m = 0;
    startTask = true;

    for (int i = 0;i<arrayLis.size();i++) {
        double latitude = ((LatLng)arrayLis.get(i)).latitude;
        double longitude = ((LatLng)arrayLis.get(i)).longitude;
        String url = "https://api.darksky.net/forecast/APIKEY/"
                +latitude+","+longitude+"?units=si";
        LatLng mylatlng = new LatLng(latitude,longitude);
        startProcess(url,mylatlng);

        Log.i("GGGTT",""+latitude+", "+longitude);
    }
}

private void startProcess(String myurl, final LatLng myLatlng){
    OkHttpClient httpClient = new OkHttpClient();
    Request request = new Request.Builder()
            .url(myurl)
            .build();

    Call call  = httpClient.newCall(request);
    call.enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            String data = response.body().string();
            Log.i("DATASS",data);
            if (response.isSuccessful()){
                try {
                    getCurrentDetails(data,myLatlng);
                } catch (JSONException e){
                    e.printStackTrace();
                }
            }
        }
    });
}

private void getCurrentDetails(String data,LatLng myLatlng) throws JSONException{
    JSONObject main = new JSONObject(data);
    double la = main.getDouble("latitude");
    double lon = main.getDouble("longitude");
    JSONObject currently = main.getJSONObject("currently");
    String summary = currently.getString("summary");
    double temperature = currently.getDouble("temperature");
    String icon = currently.getString("icon");

    LatLng latLngo = new LatLng(la,lon);
    ter.add(latLngo);
    weatherInfo.add(icon);

    // Output here is not in the same order that I have passed 

    Log.i("LETSCHECK",""+la+", "+lon +icon);
}

我传递的值:

  1. 19.21111,73.07729
  2. 19.20238,73.06582
  3. 19.19383,73.05362
  4. 19.18848,73.04221

但输出是不是在getCurrentDetails方法中相同的顺序

  1. 19.19383,73.05362
  2. 19.20238,73.06582
  3. 19.18848,73.04221
  4. 19.21111,73.07729

我觉得以前的循环被完成之前,该方法不候。

是否有获得存储在ArrayList所有地点的天气状况不改变其为了任何解决方案?

编辑

嗨,我已经通过这种方法获取的订单数据和它的做工精细,感谢,但多了一个问题了,是我期待展现数据的4倍,因为在ArrayList中4个LatLngs和它的工作很好,但是当我尝试读我已存储在另一个阵列它仅示出了2项,而不是4的取数据。

private void getCurrentDetails(String data,LatLng myLatlng) throws JSONException{

    JSONObject main = new JSONObject(data);
    double la = main.getDouble("latitude");
    double lon = main.getDouble("longitude");
    JSONObject currently = main.getJSONObject("currently");
    String summary = currently.getString("summary");
    double temperature = currently.getDouble("temperature");
    String icon = currently.getString("icon");

    //Log.i("LETSCHECK",""+la+", "+lon +icon+",k");

    loopi++;
    Log.i("LETSCHECK",""+loopi);
    if (loopi < arrayList.size()) {
        getItAgain();
    } else if (loopi == arrayList.size()){
        for (String l:weatherInfo){
            Log.i("LETSCHECK",l); 
            //expected 4 items to show but its showing only 2 items
        }
    }

    Log.i("LETSCHECK",""+la+", "+lon +icon);
    weatherInfo.add(icon); 
}

private void getItAgain() {
    double latitude = ((LatLng)arrayList.get(loopi)).latitude;
    double longitude = ((LatLng)arrayList.get(loopi)).longitude;
    String url = "https://api.darksky.net/forecast/74d8feeda5ecf5ee6667d034778b239d/"
            +latitude+","+longitude+"?units=si";
    LatLng mylatlng = new LatLng(latitude,longitude);
    startProcess(url,mylatlng);
}             
java android location okhttp weather-api
3个回答
0
投票

网络电话是异步的,这一点,你有此预期的行为。如果你想获得他们归类为你通过它们,那么你可能需要在等待前一个的网络调用的机制来完成,然后再拨打下一个。然而,这可能会增加等待服务器的响应延迟,你不是从服务器获取多个响应兼任。

如果你想等待响应,以维持排序的方式,你可能只需要做到以下几点。

// Declare a global variable of the list of your locations. 
ArrayList<LatLng> arrayLis; 
int i = 0; 

// Just start the first task without the loop. 
double latitude = ((LatLng)arrayLis.get(i)).latitude;
double longitude = ((LatLng)arrayLis.get(i)).longitude;
String url = "https://api.darksky.net/forecast/APIKEY/"
        +latitude+","+longitude+"?units=si";
LatLng mylatlng = new LatLng(latitude,longitude);
startProcess(url,mylatlng);

现在在getCurrentDetails功能,你可能只需要做到以下几点。

private void getCurrentDetails(String data,LatLng myLatlng) throws JSONException{
    JSONObject main = new JSONObject(data);
    double la = main.getDouble("latitude");
    double lon = main.getDouble("longitude");
    JSONObject currently = main.getJSONObject("currently");
    String summary = currently.getString("summary");
    double temperature = currently.getDouble("temperature");
    String icon = currently.getString("icon");

    LatLng latLngo = new LatLng(la,lon);
    ter.add(latLngo);
    weatherInfo.add(icon);

    // Now initiate the next call
    i++;
    if(i < arrayLis.size()) getItAgain();

    Log.i("LETSCHECK",""+la+", "+lon +icon);
}

public void getItAgain() {
    // Just start the first task without the loop. 
    double latitude = ((LatLng)arrayLis.get(i)).latitude;
    double longitude = ((LatLng)arrayLis.get(i)).longitude;
    String url = "https://api.darksky.net/forecast/APIKEY/"
            +latitude+","+longitude+"?units=si";
    LatLng mylatlng = new LatLng(latitude,longitude);
    startProcess(url,mylatlng);
}

更新

我不明白为什么它显示2,而不是4个结果。但是,我认为你需要如下修改代码。

private void getCurrentDetails(String data,LatLng myLatlng) throws JSONException {

    JSONObject main = new JSONObject(data);
    double la = main.getDouble("latitude");
    double lon = main.getDouble("longitude");
    JSONObject currently = main.getJSONObject("currently");
    String summary = currently.getString("summary");
    double temperature = currently.getDouble("temperature");
    String icon = currently.getString("icon");

    // Move this upto here
    weatherInfo.add(icon);

    loopi++;
    Log.i("LETSCHECK",""+loopi);
    if (loopi < arrayList.size()) {
        getItAgain();
    } else {
        for (String l:weatherInfo) {
            Log.i("LETSCHECK",l); 
            //expected 4 items to show but its showing only 2 items
        }
    }
}

0
投票

您正在运行的异步代码,并且它们的回调没有返回一个接一个的理由是合乎逻辑的,因为每个HTTP调用需要一定的时间,无关的人,所以对于你的第三个呼叫响应实际上可能之前首先接收一。

您可以使用RxJava(及其运营商),或改善当前的代码。为了您的startProcess,从您的for循环为指标通过i和我们的功能之外创建一个数组。每当你从服务器的响应,你可以将其存储在你的阵列的ith位置。每次电话后,您可以检查是否所有值都已经获取(通过计数器或东西)。这是好多了所有这些东西搬到自己的类,以便其所含的和可测试性。

需要注意的一点是,你是如何打算处理错误的,因为你的电话可能无法成功接收。


0
投票

这是一个示例代码。每个成功呼叫之后,从数组中删除当前继续项目,并执行一个另一API请求。

private void startProcess(ArrayList<LatLong> elementList){
LatLong myLatlng = elementList.get(0);
OkHttpClient httpClient = new OkHttpClient();
Request request = new Request.Builder()
        .url(myurl)
        .build();
Call call  = httpClient.newCall(request);
call.enqueue(new Callback() {
    @Override
    public void onFailure(Call call, IOException e) {

    }

    @Override
    public void onResponse(Call call, Response response) throws IOException {

        String data = response.body().string();
        Log.i("DATASS",data);
        if (response.isSuccessful()){
            try {
                getCurrentDetails(data,myLatlng);
                elementList.remove(0)
                startProcess(elementList)
            }catch (JSONException e){
                e.printStackTrace();
            }


        }
    }

});

}

© www.soinside.com 2019 - 2024. All rights reserved.