okhttp_解析数据

@Override
            public void onResponse(Call call, Response response) throws IOException {
               final String string = response.body().string();
                final User user = new Gson().fromJson(string,User.class);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.i("====login", "run: "+string);
                        String uname = user.getDatas().getUsername();
                        String uid = user.getDatas().getUserid();
                        String key = user.getDatas().getKey();
                        int code = user.getCode();
                        UserInfo userinfo = new UserInfo(uname,key);
                        // 传值
                        Bundle bundle = new Bundle();
                        bundle.putString("uname",uname);
                        Frag_my my = new Frag_my();
                        my.setArguments(bundle);
                        Log.i("======", "run: "+bundle);

                        Couments.user = userinfo;
                        if(code == 200){
                            Intent intent = new Intent(LoginActivity.this,ShouyeActivity.class);
                            startActivity(intent);
                            ToastUtil.show(LoginActivity.this,"登录成功");
                            finish();
                        }else if(code == 400){
                            ToastUtil.show(LoginActivity.this,"请重新输入");
                        }
                        Log.i("====login", "run: "+code);

                    }
                });


            }
        });
==================================================================================
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                
                final String result = response.body().string();
                Log.i("=======", "onResponse: "+result);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            JSONObject json = new JSONObject(result);
                            String code = json.getString("code");
                            if(code.equals("200")){
                                JSONObject data = json.getJSONObject("datas");
                                String uname = data.getString("username");
                                JSONObject uid = data.getJSONObject("userid");
                                String key = data.getString("key");
                                ToastUtil.show(RegActivity.this,"注册成功");
                                Intent intent = new Intent(RegActivity.this,LoginActivity.class);
                                startActivity(intent);
                                finish();
                            }else{
                                ToastUtil.show(RegActivity.this,"用户名已经存在");
                            }
                            Log.i("=======", "run: "+code);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                });

===========================================================
 
  

//单例模式来实现工具类
public class OkHttp3LUtils {
    private static OkHttpClient okHttpClient;
    //私有化的构造器
    private OkHttp3LUtils(){}
    public static OkHttpClient getInstance()
    {
        if(okHttpClient==null)
        {
            synchronized (OkHttp3LUtils.class)
            {
                //缓存的目录
                File sd_cache=new File(Environment.getExternalStorageDirectory(),"ok_cache");
                //缓存存储的大小
                int cacheSize=1024*1024*10;
                //OkHttp3拦截器
                HttpLoggingInterceptor interceptor=new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
                    @Override
                    public void log(String message) {
                        Log.i("xxx", message.toString());
                    }
                });
                //OkHttp3的拦截器日志分类  4种
                interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

                okHttpClient=new OkHttpClient.Builder()
                        .addInterceptor(interceptor)
                        .writeTimeout(30, TimeUnit.SECONDS)
                        .readTimeout(30,TimeUnit.SECONDS)
                        .connectTimeout(60,TimeUnit.SECONDS)
                        .cache(new Cache(sd_cache,cacheSize))
                        .build();
            }
        }
        return okHttpClient;
    }
    //get请求
    public static void doGet(String url, Callback callback)
    {
        OkHttpClient okHttpClient=getInstance();
        //创建Request
        Request request=new Request.Builder()
                .url(url)
                .build();
        Call call=okHttpClient.newCall(request);
        call.enqueue(callback);
    }
    //post请求
    public static void doPost(String url, Map params, Callback callback)
    {
        OkHttpClient okHttpClient=getInstance();

        FormBody.Builder builder=new FormBody.Builder();
        for(String key:params.keySet())
        {
            builder.add(key,params.get(key));
        }

        Request request=new Request.Builder()
                .post(builder.build())
                .url(url)
                .build();

        Call call=okHttpClient.newCall(request);
        call.enqueue(callback);
    }
}


 
  


你可能感兴趣的:(okhttp)