Retrofit简单了解

package com.example.lianxi;

/**
 * Created by HP on 2017/11/4.
 */

public class Contanst {
    public static final String BASE_URL= "http://v.juhe.cn/";
}

=====================================

package com.example.lianxi;

import retrofit2.Call;
import retrofit2.http.GET;

/**
 * Created by HP on 2017/11/4.
 */

public interface Utils {
    @GET("toutiao/index?type=top&key=6535f9a621cb3661b96f538ea740c9b6")
    Call<Bean> getLat();
    @GET("toutiao/index?type=top&key=6535f9a621cb3661b96f538ea740c9b6")
    Call<Bean> getLatss();

}
================、

package com.example.lianxi;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class MainActivity extends AppCompatActivity {
    private ListView mylist;
    private Adapter ad;
    private List<Bean> list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(Contanst.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())   //实现Gson解析
                .build();
        Utils utils=retrofit.create(Utils.class);
        utils.getLatss().enqueue(new Callback<Bean>() {
            @Override
            public void onResponse(Call<Bean> call, Response<Bean> response) {
                Bean bean=response.body();
                list.add(bean);
                ad=new Adapter(MainActivity.this,list);
                mylist.setAdapter(ad);
            }

            @Override
            public void onFailure(Call<Bean> call, Throwable t) {

            }
        });
    }
    private void initView() {
        mylist = (ListView) findViewById(R.id.mylist);

    }
}
=====================
package com.example.lianxi;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by HP on 2017/11/4.
 */

public class Adapter extends BaseAdapter {
    Context context;
    List<Bean> list = new ArrayList<>();

    public Adapter(Context context, List<Bean> list) {
        this.context = context;
        this.list = list;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Viewholder holder = null;
        if(view == null){
            holder = new Viewholder();
            view = View.inflate(context,R.layout.item,null);
            holder.img = view.findViewById(R.id.myimg);
            holder.tv = view.findViewById(R.id.mytv);
            view.setTag(holder);
        }else{
            holder = (Viewholder) view.getTag();
        }
        Glide.with(context).load(list.get(i).getResult().getData().get(i).getThumbnail_pic_s()).into(holder.img);
        holder.tv.setText(list.get(i).getResult().getData().get(i).getTitle());

        return view;
    }
    class Viewholder{
        ImageView img;
        TextView tv;
    }
}
=====================

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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" tools:context="com.example.lianxi.MainActivity">

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

</RelativeLayout>
==================

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:id="@+id/myimg"
            android:background="@mipmap/ic_launcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/mytv"
            android:text="5555"
            android:textSize="20sp"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
    </LinearLayout>
</LinearLayout>
=====================
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.lianxi">

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
 =================
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile 'com.github.bumptech.glide:glide:3.7.0'
    compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值