OKHttp_POST和Get 请求
来源:互联网 发布:二维字符串数组赋值 编辑:程序博客网 时间:2024/06/06 23:58
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(); }}
阅读全文
0 0
- OKHttp_POST和Get 请求
- get请求和post请求
- get请求和post请求
- GET请求和POST请求
- GET请求和POST请求
- GET请求和POST请求
- GET请求和POST请求
- GET请求和POST请求
- POST请求和GET请求
- get请求和post请求
- GET和POST请求
- Get和Post请求
- post和get请求
- Get和Post请求
- post和get请求
- get和post请求
- get和post请求
- post和get请求
- php、yii2使用过程中的一些技巧
- Spring Boot 运行原理之自动配置
- Vmware FAQ
- RxJava之Subject分类解析
- 防止SQL注入的五种方法
- OKHttp_POST和Get 请求
- sublime 的key binding 设置删除行
- PAT(Basic Level)_1026_程序运行时间
- hdoj2011_多项式求和
- Python中file()和open()的区别
- LeetCode(19)--Remove Nth Node From End of List
- Wireless Network
- js(jQuery)获取时间的方法及常用时间类
- CodeForces 580 D.Kefa and Dishes(状压DP)