Retrofit简单了解
来源:互联网 发布:电子传单制作软件 编辑:程序博客网 时间:2024/06/06 01:05
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'}
阅读全文
0 0
- Retrofit简单了解
- Retrofit和Rxjava的简单了解
- Retrofit初了解
- Retrofit的简单使用
- retrofit 2.0 简单使用
- Retrofit的简单使用
- Retrofit+RxJava 简单学习
- Rxjava+Retrofit 简单使用
- Retrofit的简单使用
- Retrofit的简单使用方法
- Retrofit的简单应用
- Retrofit简单原理分析
- Retrofit的简单使用方法
- Retrofit简单使用
- Retrofit简单入门
- Retrofit简单尝试
- Retrofit简单使用手册
- Retrofit简单封装
- Retrofit的使用
- Java并发编程:Thread类的使用
- MySQL学习之一 :ubuntu下 MySQL的安装与基本操作
- Bear and Poker CodeForces
- PAT乙级题1025.反转链表
- Retrofit简单了解
- C语言知识点总结
- xzwfragment显示
- 线性表的链式表示和实现
- linux 后台运行命令 nohup命令
- MySQL免安装版中 my-default.ini 的配置
- python中关于dir()
- Spring AOP原理 AOP两种配置方式
- 621. Task Scheduler