okgo简单的GET请求 和POST请求

转载:http://blog.csdn.net/bskfnvjtlyzmv867/article/details/71598263#comments

URL是从https://github.com/jeasonlzy/okhttp-OkGo  demo里面拿的。

package com.example.administrator.okgo_2;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import okhttp3.Call;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button get_id;
    private Button post_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        IntView();
    }
    private void IntView() {
        get_id = (Button) findViewById(R.id.get_id);
        post_id = (Button)  findViewById(R.id.post_id);
        get_id.setOnClickListener(this);
        post_id.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        String url="http://server.jeasonlzy.com/OkHttpUtils/method";
        switch (v.getId()){

            case R.id.get_id:

                OkGoGet(url);

                break;
            case R.id.post_id:

                OkGoPost(url);
                break;
        }
    }


    private void OkGoPost(String url) {
        Log.i("数据:","==1"+url);
        OkGo.post(url)
                .tag(this)
                .cacheKey("cachePostKey")
                .cacheMode(CacheMode.DEFAULT)
                .params("param2", "paramValue2")
                .params("param3", "paramValue3")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.i("数据:","==2"+s);
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        Log.i("数据:","==2"+e.getMessage());
                    }
                });


    }

    private void OkGoGet(String url) {
        Log.i("数据:","==1"+url);
        OkGo.get(url)                            // 请求方式和请求url
                .tag(this)                       // 请求的 tag, 主要用于取消对应的请求
                .cacheKey("cacheKey")            // 设置当前请求的缓存key,建议每个不同功能的请求设置一个
                .cacheMode(CacheMode.DEFAULT)    // 缓存模式,详细请看缓存介绍
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                    }
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Log.i("数据:","==2"+s);

                    }
                });


    }


}



你可能感兴趣的:(okgo简单的GET请求 和POST请求)