您的位置:首页 > 理论基础 > 计算机网络

Android进阶之路 - okHttp中Get与Post初级使用(二)

2017-04-28 17:37 525 查看
因前一篇并没有把具体的效果图展现,故本篇讲解的同样是okHttp下的Get请求与Post请求,不包含文件与图片的上传与下载(之后可能在中级使用中讲到)

前提 (使用了三方平台的接口) :



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