Android开发之开源框架OKHTTP的Get请求代码,得到json字符串方法

 
<span style="white-space:pre">	</span><pre name="code" class="java">/**
 * Created by David Zheng on 2016/3/30.
 * <p/>
 * Qq:986945193
 * <p/>
 * 微博:http://weibo.com/mcxiaobing
 */


 /**
     * 异步获取到数据 开启了子线程
     *
     * 若是同步只需将enqueue 改为execute即可
     *
     * @return
     */
    public String getUrlfromJson() {

        OkHttpClient mHttpClient = new OkHttpClient();

        Request request = new Request.Builder().url(url)
                .build();
        mHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {
                //加载失败
            }

            @Override
            public void onResponse(Response response) throws IOException {
                //成功获取数据
                if (response.isSuccessful()){
                    String json = response.body().toString();
                    Log.d("mainai", "onResponse() returned: " + json);

                }
            }
        });
        
        return url;
    }


程序员小冰博客:http://blog.csdn.net/qq_21376985 技术交流QQ986945193 微博:http://weibo.com/mcxiaobing
原文地址:https://www.cnblogs.com/mcxiaobing/p/5472093.html