Android进阶之路 - okHttp中Get与Post初级使用(二)
2017-04-28 17:37
525 查看
因前一篇并没有把具体的效果图展现,故本篇讲解的同样是okHttp下的Get请求与Post请求,不包含文件与图片的上传与下载(之后可能在中级使用中讲到)
前提 (使用了三方平台的接口) :
Effect :
还有一些主要注意,可通过上一篇okHttp查看,链接地址如下:
MainActivity :
MainActivity Xml:
前提 (使用了三方平台的接口) :
Effect :
还有一些主要注意,可通过上一篇okHttp查看,链接地址如下:
http://blog.csdn.net/qq_20451879/article/details/55001505
MainActivity :
package com.example.dow.okpost; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.TextView; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class MainActivity extends AppCompatActivity { private TextView mContent; private TextView mBtn1; private TextView mBtn2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final String PROJECT_KEY="a79a913e685b1e7c"; //这里post请求用到了此URL final String URL="http://api.jisuapi.com/train/station2s?appkey="+PROJECT_KEY; mBtn1 = (TextView) findViewById(R.id.tv_Btn1); mBtn2 = (TextView) findViewById(R.id.tv_Btn2); mContent = (TextView) findViewById(R.id.tv_content); mBtn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { OkHttpClient okHttpClient = new OkHttpClient(); FormBody Body = new FormBody.Builder() .add("start","上海") .add("end","北京") .add("ishigh","0").build(); Request request = new Request.Builder().post(Body).url(URL).build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { final String result = response.body().string(); runOnUiThread(new Runnable() { @Override public void run() { mContent.setText(result); } }); } }); } }).start(); } }); mBtn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { // 1.创建实例对象 OkHttpClient getOkHttp = new OkHttpClient(); // 2-1.创建请求信息-先创建请求的容器.在填充请求的数据,并提交 final Request request = new Request.Builder() .url("http://api.jisuapi.com/train/station2s?appkey=a79a913e685b1e7c&start=杭州&end=北京&ishigh=0") .build(); // 3.使用okhttp的实例发送请求,并执行,回调 Call newCall = getOkHttp.newCall(request); newCall.enqueue(new Callback() { public String responseData; @Override public void onResponse(Call arg0, Response arg1) throws IOException { //获取请求结果 responseData = arg1.body().string(); //因为我们要让UI改变,所以在主线程改变UI runOnUiThrea 4000 d(new Runnable() { @Override public void run() { mContent.setText(responseData); } }); } @Override public v ilure(Call arg0, IOException arg1) { } } }).start(); } }); } }
MainActivity Xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.dow.okpost.MainActivity"> <TextView android:layout_width="match_parent" android:padding="8dp" android:gravity="center" android:id="@+id/tv_Btn1" android:layout_height="wrap_content" android:text="Post查询数据" /> <View android:layout_width="match_parent" android:layout_height="2dp" android:background="#2b2b2b" /> <TextView android:layout_width="match_parent" android:padding="8dp" android:gravity="center" android:id="@+id/tv_Btn2" android:layout_height="wrap_content" android:text="Get查询数据" /> <View android:layout_width="match_parent" android:layout_height="2dp" android:background="#2b2b2b" /> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/tv_content" /> </LinearLayout>
相关文章推荐
- Android进阶之路 - okHttp中Get与Post初级使用(一)
- 使用基于Android网络通信的OkHttp库实现Get和Post方式简单操作服务器JSON格式数据
- android 基于OkHttp GET 和 POST 的使用
- Android中使用OkHttp包处理HTTP的get和post请求的方法
- android okhttp的简单使用(get,post)
- Android中Okhttp进行get和post提交及数据请求的基本使用
- Android之HttpPost与HttpGet使用
- Android中使用HttpGet和HttpPost访问HTTP资源(二)
- Android中使用HttpGet和HttpPost访问HTTP资源和区别
- android开发 使用HttpClient访问网络,post、get方式。注册、登陆、上传文件。
- android中使用get、post和HttpClient方式与服务器交互
- Android使用HTTP GET、HTTP POST获取网络数据
- Android中使用HttpGet和HttpPost访问HTTP资源(一)
- android 网络编程 HttpGet和HttpPost类使用详解
- Android使用Apache HttpClient发送GET、POST请求
- Android使用Apache HttpClient发送GET、POST请求
- Android 使用三种方式获取网页(通过Post,Get进行表单的提交)
- android 网络编程 HttpGet和HttpPost类使用详解
- Android 使用 HttpClient 进行网络通信,包括Get方式和Post方式
- Android HTTP实例 使用GET方法和POST方法发送请求