OKHttp get请求数据

OKHttp get请求数据_第1张图片

准备工作:

导依赖
compile ‘com.squareup.okhttp3:okhttp:3.4.1’
compile ‘com.squareup.okio:okio:1.5.0’
compile ‘com.google.code.gson:gson:2.6.2’

加权限



activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.administrator.day_082.MainActivity">
    <Button
        android:id="@+id/get"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="get请求"/>

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    ListView>

LinearLayout>

item.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:id="@+id/wen"
        android:layout_width="match_parent"
        android:text="5"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/wen1"
        android:layout_width="match_parent"
        android:text="5"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/wen2"
        android:layout_width="match_parent"
        android:text="5"
        android:layout_height="wrap_content" />
LinearLayout>

OKHttpUitls.java

package com.example.administrator.day_082;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    String url="http://v.juhe.cn/weather/index?format=2&cityname=%E8%8B%8F%E5%B7%9E&key=3bc9d500c39eecdcd4d01a3cd324df22";
    private List list;
    private Button get;
    private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();

    }
    private void initViews() {
        get = (Button) findViewById(R.id.get);
        get.setOnClickListener(this);
        lv = (ListView) findViewById(R.id.listview);
    }
    public void onClick(View v){
        //创建OKHttpClient对象
        OKHttpUitls okHttpUitls = new OKHttpUitls();
        okHttpUitls.get(url);
        okHttpUitls.setOnOkHttpGetListener(new OKHttpUitls.OKHttpGetListener() {
            @Override
            public void error(String error) {
                Toast.makeText(MainActivity.this,error,Toast.LENGTH_SHORT).show();
            }

            @Override
            public void success(String json) {
                Toast.makeText(MainActivity.this,json,Toast.LENGTH_SHORT).show();
                Gson gson = new Gson();
                Bean fromJson = gson.fromJson(json, Bean.class);
                list = fromJson.getResult().getFuture();
                MyAdapter adapter = new MyAdapter(MainActivity.this,list);
                lv.setAdapter(adapter);
            }
        });
    }
    class MyAdapter extends BaseAdapter{
        private Context context;
        private List list;

        public MyAdapter(Context context, List list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(context, R.layout.item, null);
            TextView wen = (TextView) view.findViewById(R.id.wen);
            TextView wen2 = (TextView) view.findViewById(R.id.wen2);
            TextView wen1 = (TextView) view.findViewById(R.id.wen1);
            wen1.setText(list.get(position).getWeather());
            wen.setText(list.get(position).getTemperature());
            wen2.setText(list.get(position).getDate());
            return view;
        }
    }
}

MainActivity.java

package com.example.administrator.day_082;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    String url="http://v.juhe.cn/weather/index?format=2&cityname=%E8%8B%8F%E5%B7%9E&key=3bc9d500c39eecdcd4d01a3cd324df22";
    private List list;
    private Button get;
    private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();

    }
    private void initViews() {
        get = (Button) findViewById(R.id.get);
        get.setOnClickListener(this);
        lv = (ListView) findViewById(R.id.listview);
    }
    public void onClick(View v){
        //创建OKHttpClient对象
        OKHttpUitls okHttpUitls = new OKHttpUitls();
        okHttpUitls.get(url);
        okHttpUitls.setOnOkHttpGetListener(new OKHttpUitls.OKHttpGetListener() {
            @Override
            public void error(String error) {
                Toast.makeText(MainActivity.this,error,Toast.LENGTH_SHORT).show();
            }

            @Override
            public void success(String json) {
                Toast.makeText(MainActivity.this,json,Toast.LENGTH_SHORT).show();
                Gson gson = new Gson();
                Bean fromJson = gson.fromJson(json, Bean.class);
                list = fromJson.getResult().getFuture();
                MyAdapter adapter = new MyAdapter(MainActivity.this,list);
                lv.setAdapter(adapter);
            }
        });
    }
    class MyAdapter extends BaseAdapter{
        private Context context;
        private List list;

        public MyAdapter(Context context, List list) {
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(context, R.layout.item, null);
            TextView wen = (TextView) view.findViewById(R.id.wen);
            TextView wen2 = (TextView) view.findViewById(R.id.wen2);
            TextView wen1 = (TextView) view.findViewById(R.id.wen1);
            wen1.setText(list.get(position).getWeather());
            wen.setText(list.get(position).getTemperature());
            wen2.setText(list.get(position).getDate());
            return view;
        }
    }
}

你可能感兴趣的:(OKHttp get请求数据)