Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取loop android内部多个位置的天气状态_Java_Android_Location_Okhttp_Weather Api - Fatal编程技术网

Java 获取loop android内部多个位置的天气状态

Java 获取loop android内部多个位置的天气状态,java,android,location,okhttp,weather-api,Java,Android,Location,Okhttp,Weather Api,我正在开发一个天气应用程序,我正在使用dark sky API,我想知道我存储在ArrayList中的一系列位置的天气状态 我使用OKHttp解析API中的JSON数据,因此我尝试在for循环中循环整个获取过程,但它没有给出所需的输出 private void beginTask(ArrayList<LatLng> arrayLis) { //arraylis contains list of locations(LatLng) m = 0; startTa

我正在开发一个天气应用程序,我正在使用dark sky API,我想知道我存储在
ArrayList
中的一系列位置的天气状态

我使用
OKHttp
解析API中的JSON数据,因此我尝试在for循环中循环整个获取过程,但它没有给出所需的输出

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);
}

您正在运行异步代码,而它们的回调没有一个接一个地返回的原因是合乎逻辑的,因为每个HTTP调用都需要一定的时间,与其他调用无关,因此第三个调用的响应实际上可能在第一个调用之前收到

您可以使用RxJava(及其运算符),也可以改进当前代码。对于
startProcess
,将For循环中的
i
作为索引传递,并在函数外部创建一个数组。无论何时从服务器获得响应,您都可以将其存储在阵列的
i
th位置。每次调用后,您可以检查是否已提取所有值(通过计数器或其他方式)。最好将所有这些内容移动到它自己的类中,这样它就可以被包含和测试


需要注意的一点是,您将如何处理错误,因为您的呼叫可能无法成功接收。

您正在运行异步代码,并且它们的回调不会一个接一个地返回的原因是合乎逻辑的,因为每个HTTP调用都需要一定的时间,与其他调用无关,因此,第三次呼叫的响应可能会在第一次呼叫之前收到

您可以使用RxJava(及其运算符),也可以改进当前代码。对于
startProcess
,将For循环中的
i
作为索引传递,并在函数外部创建一个数组。无论何时从服务器获得响应,您都可以将其存储在阵列的
i
th位置。每次调用后,您可以检查是否已提取所有值(通过计数器或其他方式)。最好将所有这些内容移动到它自己的类中,这样它就可以被包含和测试


需要注意的一点是,您将如何处理错误,因为您的呼叫可能无法成功接收。

网络呼叫是异步的,这是您在这里的预期行为。如果您希望在传递它们时对它们进行排序,那么您可能需要一种机制,即等待前一个网络调用完成,然后再次调用下一个网络调用。但是,这可能会增加等待服务器响应的延迟,因为您不会同时从服务器获取多个响应

如果要等待响应以保持排序方式,可能只需要执行以下操作

// 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);
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
        }
    }
}
private void getCurrentDetails(字符串数据,LatLng myLatlng)抛出JSONException{
JSONObject main=新的JSONObject(数据);
双la=main.getDouble(“纬度”);
double lon=main.getDouble(“经度”);
JSONObject当前=main.getJSONObject(“当前”);
字符串摘要=当前.getString(“摘要”);
双温=当前.getDouble(“温度”);
字符串图标=当前.getString(“图标”);
//把这个搬到这里来
weatherInfo.add(图标);
loopi++;
Log.i(“LETSCHECK”和“+loopi”);
if(loopi
网络调用是异步的,这是您在这里的预期行为。如果您希望在传递它们时对它们进行排序,那么您可能需要一种机制,即等待前一个网络调用完成,然后再次调用下一个网络调用。但是,这可能会增加等待服务器响应的延迟,因为您不会同时从服务器获取多个响应

如果要等待响应以保持排序方式,可能只需要执行以下操作

// 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);
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
        }
    }
}
private void getCurrentDetails(字符串数据,LatLng myLatlng)抛出JSONException{
JSONObject main=新的JSONObject(数据);
双la=main.getDouble(“纬度”);
double lon=main.getDouble(“经度”);
JSONObject当前=main.getJSONObject(“当前”);
字符串摘要=当前.getString(“摘要”);
双温=当前.getDouble(“温度”);
字符串图标=当前.getString(“图标”);
//把这个搬到这里来
weatherInfo.add(图标);
loopi++;
Log.i(“LETSCHECK”和“+loopi”);
if(loopi
这是一个示例代码。每次成功调用后,从数组中删除当前的继续项,并执行另一个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();
            }


        }
    }

});
private void startProcess(ArrayList元素列表){
LatLong myLatlng=elementList.get(0);
OkHttpClient httpClient=新的OkHttpClient();
Request Request=newrequest.Builder()
.url(myurl)
.build();
Call Call=httpClient.newCall(请求);
call.enqueue(新回调(){
@凌驾
公共void onFailure(调用调用,IOE异常){
}
@凌驾
public void onResponse(调用调用、响应响应)引发IOException{
字符串数据=response.body().String();
Log.i(“数据”,数据);
if(response.issusccessful()){
试一试{