Android安卓使用okHttp实现post,get请求,并解析数据

时间:2024-03-12 21:36:41

Android安卓使用okHttp实现post,get请求,并解析数据

  1. 添加okhttp库的依赖

compile 'com.squareup.okhttp3:okhttp:3.2.0'

Android安卓使用okHttp实现post,get请求,并解析数据

 

  1. 修改activity_main.xmlAndroid安卓使用okHttp实现post,get请求,并解析数据

 

<?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.okhttp.MainActivity">
    <!--button控件用于点击提交-->
    <Button
        android:id="@+id/submit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="提交"/>
    <!--scrollView控件用于溢出滚动显示-->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!--TextView用于将请求地址返回的数据显示出来-->
        <TextView
            android:id="@+id/text1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="显示"
            />
    </ScrollView>
</LinearLayout>

  1. 修改MainActivity中的代码

 

Android安卓使用okHttp实现post,get请求,并解析数据

package com.example.okhttp;

import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    TextView text1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button submit=(Button) findViewById(R.id.submit);
        text1=(TextView) findViewById(R.id.text1);
        submit.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.submit){
            //点击之后执行请求
            submit();
        }

    }
    private void submit(){
        //创建一个子线程进行逻辑处理
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {

                    //get方式开始

//                    OkHttpClient client =new OkHttpClient();
//                    Request request=new Request.Builder().url("http://www.baidu.com").build();
//                    //使用newcall()创建一个call对象,并调用
//                   Response response= client.newCall(request).execute();
//                    //得到返回数据
//                   String data= response.body().string();
//                    //将返回数据添加到ui页面上
//                    showData(data);


                    //get方式结束



                    //post方式开始
                    OkHttpClient client1 =new OkHttpClient();
                    FormBody formBody=new FormBody.Builder().add("name","小明").add("pas","123456").build();
                    Request request1=new Request.Builder()
                            .url("http://www.baidu.com")
                            .post(formBody)
                            .build();

                    Response response1= client1.newCall(request1).execute();
                    //得到返回数据
                    String data1= response1.body().string();
                    //将返回数据添加到ui页面上
                    showData(data1);
                    //post方式结束


                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void showData(final String data){
        //切换回主线程处理,安卓不允许在子线程设置ui界面,所以需要回到主线程
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this,"你好2",Toast.LENGTH_SHORT).show();
                text1.setText(data);
            }
        });

    }
}