Android:OKHTTP_POST和GET 请求

OKHttp_POST和Get 请求

Download

1、 v3.8.1 JAR

2、You’ll also need Okio, which OkHttp uses for fast I/O and resizable buffers. Download the latest JAR.

The source code to OkHttp, its samples, and this website is available on GitHub.

https://github.com/YoungManZheng/okhttp.git

package example.com.okhttp;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class Okhttp_MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final int GET = 1;  //get请求
    private static final int POST = 2; //psot请求
    private Button btn_get_post;
    private TextView tv_result;
    private OkHttpClient client = new OkHttpClient();
    public static final MediaType JSON
            = MediaType.parse("application/json; charset=utf-8");

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case GET:
                    //获取数据
                    tv_result.setText((String) msg.obj);

                case POST:
                    //获取数据
                    tv_result.setText((String) msg.obj);

                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_okhttp__main);
        btn_get_post = (Button) findViewById(R.id.btn_get_post);
        tv_result = (TextView) findViewById(R.id.tv_result);

        btn_get_post.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_get_post:  //使用原声的okhtpp请求数据 get post
                tv_result.setText("");
                getDataFromPost();
                break;
        }
    }

    /**
     * 使用get请求网络
     */
    private void getDataFromGet() {
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    String result = get("http://www.json.cn/code.html");
                    Log.e("Tag", result);
                    Message msg = Message.obtain();
                    msg.what = GET;
                    msg.obj = result;
                    handler.sendMessage(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }

    /**
     * 使用Post请求网络
     */
    private void getDataFromPost() {
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    String result = post("http://blog.sina.com.cn/s/blog_9ac333de0102vsxf.html", "");
                    Log.e("Tag", result);
                    Message msg = Message.obtain();
                    msg.what = POST;
                    msg.obj = result;
                    handler.sendMessage(msg);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }




    /**
     * get请求
     *
     * @param url
     * @return
     * @throws IOException
     */
    private String get(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .build();

        Response response = client.newCall(request).execute();
        return response.body().string();
    }


    /**
     * post请求
     */

    private String post(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }


}

没有更多推荐了,返回首页