在Java / Android 中执行网络活动 的最快,最干净,最简单的方法是什么,而不必处理AsyncTask,Threads和其他gulp的混乱?是否可能有一个库来处理程序员的大部分工作?
我目前的目标是让API-End-Point给我一个JSON字符串,我可以把它放到地图上做各种各样的事情。
到目前为止这是代码:
Map<String, String> launch;
private static String url = "https://api.spacexdata.com/v2/launches/latest";
private JSONObject jsonObject;
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
Response response = null;
response = okHttpClient.newCall(request).execute();
String informationString = response.body().string().toString();
/* Just grabing the information so really uninteresting */
jsonObject = new JSONObject(informationString);
launch.put("missionName", jsonObject.get("mission_name").toString());
launch.put("rocketName", jsonObject.getJSONObject("rocket").get("rocket_name").toString());
launch.put("block", jsonObject.getJSONObject("rocket").getJSONObject("firstfirst_stage").getJSONArray("cores").getString(3));
launch.put("flightNumber", jsonObject.get("flight_number").toString());
我得到的错误是NetworkOnMainThreadActivity 。我已经尝试了很多东西来避免/解决它,但最终只会出现更多与此无关的例外情况。
W/System.err: android.os.NetworkOnMainThreadException
W/System.err: at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1448)
W/System.err: at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:102)
W/System.err: at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:90)
W/System.err: at java.net.InetAddress.getAllByName(InetAddress.java:787)
W/System.err: at okhttp3.Dns$1.lookup(Dns.java:40)
W/System.err: at okhttp3.internal.connection.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:185)
W/System.err: at okhttp3.internal.connection.RouteSelector.nextProxy(RouteSelector.java:149)
W/System.err: at okhttp3.internal.connection.RouteSelector.next(RouteSelector.java:84)
W/System.err: at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:213)
W/System.err: at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:134)
W/System.err: at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:113)
W/System.err: at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
W/System.err: at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
W/System.err: at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err: at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
W/System.err: at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
W/System.err: at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
W/System.err: at okhttp3.RealCall.execute(RealCall.java:77)
W/System.err: at com.pushfurther.spacelaunchtracker.MainActivity.onCreate(MainActivity.java:31)
W/System.err: at android.app.Activity.performCreate(Activity.java:7174)
W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
W/System.err: at android.app.ActivityThread.-wrap11(Unknown Source:0)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:105)
W/System.err: at android.os.Looper.loop(Looper.java:164)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:6938)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
答案 0 :(得分:1)