我正在使用okHttp 我的网址没问题 但是当我在模拟器或真实设备上运行该应用程序时,它永远不会显示任何错误。但它没有记录天气数据。
Mainactivity.java
package com.buckydroid.droidweather;
import android.app.DownloadManager;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
public static String responsedata;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text = (TextView)findViewById(R.id.text) ;
new AsyncTask<Void, Void, String>() {
@Override
protected String doInBackground(Void... params) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("https://api.forecast.io/forecast/11f696255d54c7cea3d097a6338bbaed/37.8267,-122.423").build();
try {
Response response = client.newCall(request).execute();
responsedata = response.body().toString();
Log.d(TAG,responsedata);
return responsedata;
} catch (IOException e) {
e.printStackTrace();
}
return responsedata;
}
}.execute();
}
}
LOG
08-04 17:27:31.847 18694-18694/com.buckydroid.droidweather I/art: Not late-enabling -Xcheck:jni (already on)
08-04 17:27:31.936 18694-18694/com.buckydroid.droidweather W/System: ClassLoader referenced unknown path: /data/app/com.buckydroid.droidweather-2/lib/x86
08-04 17:27:52.479 18694-18694/com.buckydroid.droidweather W/System: ClassLoader referenced unknown path: /data/app/com.buckydroid.droidweather-2/lib/x86
08-04 17:27:52.562 18694-18694/com.buckydroid.droidweather W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-04 17:27:52.603 18694-19012/com.buckydroid.droidweather D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
[ 08-04 17:27:52.613 18694:18694 D/ ]
HostConnection::get() New Host Connection established 0xaa99da20, tid 18694
[ 08-04 17:27:52.668 18694:19012 D/ ]
HostConnection::get() New Host Connection established 0xae407270, tid 19012
08-04 17:27:52.671 18694-19012/com.buckydroid.droidweather I/OpenGLRenderer: Initialized EGL, version 1.4
08-04 17:27:54.061 18694-18694/com.buckydroid.droidweather W/art: Verification of boolean android.support.v7.widget.ActionMenuPresenter.isOverflowReserved() took 180.240ms
08-04 17:27:56.127 18694-19011/com.buckydroid.droidweather D/MainActivity: okhttp3.internal.http.RealResponseBody@fe12d95
我添加了互联网许可。那个没问题
答案 0 :(得分:0)
这个片段:
Log.d(TAG,responsedata);
responsedata = response.body().string();
它应该是相反的顺序
接下来,您需要根据String
来回doInBackground
AsyncTask<Void, Void, String>
另外,常见的做法是扩展AsyncTask
并使用自己的类。