简体   繁体   English

Android…使用Okhttp无法从Darksky Weather API中获得JSON响应

[英]Android…Not getting JSON respone from darksky weather API using Okhttp

I am writing code to build a simple weather app using Darksky API Following is my java code file. 我正在编写代码以使用Darksky API构建简单的天气应用程序,以下是我的Java代码文件。 In which I am using okHttp 3.5.0 and asynchronously calling the darksky API and writing response on the log. 我在其中使用okHttp 3.5.0,并异步调用darksky API并在日志上写入响应。

MainActivity.java MainActivity.java

package treehouse.com.stormy;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import java.io.IOException;

import javax.xml.datatype.Duration;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    public static final String TAG=MainActivity.class.getSimpleName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        double latitude=37.8267;
        double longitude=-122.4233;
        String apiKey="Here you would insert your key and I have ofcourse mine";
        String forecastURL="https://api.darksky.net/forecast/" + apiKey + "/" + latitude + "," + longitude;

      OkHttpClient client=new OkHttpClient();
        Request request=new Request.Builder().url(forecastURL).build();
        Call call=client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

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

                try {
                    if(response.isSuccessful()) {
                        Log.v(TAG, response.body().string());
                    }
                } catch (IOException e) {

                    Log.e(TAG,"Exception caught: ",e);
                }
            }
        });


    }
}

Activity Output 活动输出

在此处输入图片说明

Gradle 摇篮

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "25.0.1"
    defaultConfig {
        applicationId "treehouse.com.stormy"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.squareup.okio:okio:1.6.0'
    compile project(':okhttp-3.5.0')
}
repositories {
    mavenCentral()

}

Detailed Logcat 详细的Logcat

Logcat logcat的

**My activity is crashing on the emulator but it is correctly calling the API and I have checked the API console usage before and after running the code? **我的活动在模拟器上崩溃了,但是它正确地调用了API,并且在运行代码之前和之后我都检查了API控制台的使用情况吗?

API usage before running the code 运行代码之前的API使用情况

在此处输入图片说明

API usage after running the code 运行代码后的API使用情况

在此处输入图片说明

You are using an older version of Okio library which is not compatible with your version of OkHttp, causing the crash when trying to call a nonexistent method. 您使用的Okio库的旧版本与您的OkHttp版本不兼容,从而在尝试调用不存在的方法时导致崩溃。 I would advise using the latest version of Okio, but the OkHttp maven library already has the dependency, too. 我建议使用最新版本的Okio,但是OkHttp maven库也已经具有依赖项。

Just remove the compile 'com.squareup.okio:okio:1.6.0' line from your gradle file. 只需从gradle文件中删除compile 'com.squareup.okio:okio:1.6.0'行即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM