原CSDN博客已弃用,文章会逐渐迁移过来。

一、GET请求列表数据展示

添加依赖

dependencies {  
    compile 'com.squareup.picasso:picasso:2.5.2'  
    compile 'com.squareup.retrofit2:retrofit:2.0.2'  
    compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'  
}  

item.xml

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

    <ImageView  
        android:id="@+id/item_image"  
        android:layout_width="70dp"  
        android:layout_height="70dp" />  
    <LinearLayout  
        android:layout_width="0dp"  
        android:layout_height="wrap_content"  
        android:orientation="vertical"  
        android:layout_weight="1">  
        <TextView  
            android:id="@+id/item_title"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:textAppearance="@android:style/TextAppearance.Large"/>  
        <TextView  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:id="@+id/item_info"  
            android:maxLines="2"  
            android:ellipsize="end"/>  
    </LinearLayout>  
</LinearLayout>  

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>  
<android.support.constraint.ConstraintLayout  
    xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context="com.administrator.picassodemo.MainActivity">  


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

    </ListView>  


</android.support.constraint.ConstraintLayout>  

数据实体类

Cook.java

package com.administrator.picassodemo;  

import com.google.gson.annotations.SerializedName;  

/** 
 * Created by Administrator on 2018/5/16. 
 */  

public class Cook {  

    @SerializedName("id")  
    private int id;  
    @SerializedName("name")  
    private String name;  
    @SerializedName("food")  
    private String food;  
    @SerializedName("img")  
    private String img;  
    @SerializedName("images")  
    private String images;  
    @SerializedName("description")  
    private String description;  
    @SerializedName("keywords")  
    private String keywords;  
    @SerializedName("message")  
    private String message;  
    @SerializedName("count")  
    private int count;  
    @SerializedName("fcount")  
    private int fcount;  
    @SerializedName("rcount")  
    private int rcount;  

    public int getId() {  
        return id;  
    }  

    public void setId(int id) {  
        this.id = id;  
    }  

    public String getName() {  
        return name;  
    }  

    public void setName(String name) {  
        this.name = name;  
    }  

    public String getFood() {  
        return food;  
    }  

    public void setFood(String food) {  
        this.food = food;  
    }  

    public String getImg() {  
        return img;  
    }  

    public void setImg(String img) {  
        this.img = img;  
    }  

    public String getImages() {  
        return images;  
    }  

    public void setImages(String images) {  
        this.images = images;  
    }  

    public String getDescription() {  
        return description;  
    }  

    public void setDescription(String description) {  
        this.description = description;  
    }  

    public String getKeywords() {  
        return keywords;  
    }  

    public void setKeywords(String keywords) {  
        this.keywords = keywords;  
    }  

    public String getMessage() {  
        return message;  
    }  

    public void setMessage(String message) {  
        this.message = message;  
    }  

    public int getCount() {  
        return count;  
    }  

    public void setCount(int count) {  
        this.count = count;  
    }  

    public int getFcount() {  
        return fcount;  
    }  

    public void setFcount(int fcount) {  
        this.fcount = fcount;  
    }  

    public int getRcount() {  
        return rcount;  
    }  

    public void setRcount(int rcount) {  
        this.rcount = rcount;  
    }  
}  

返回的数据有集合数据,所以建立集合数据实体类

Tngou.java

package com.administrator.picassodemo;  

import com.google.gson.annotations.SerializedName;  

import java.util.List;  

/** 
 * Created by Administrator on 2018/5/16. 
 */  

public class Tngou {  
    @SerializedName("status")  
    private boolean status;  
    @SerializedName("total")  
    private int total;  
    @SerializedName("tngou")  
    private List<Cook> list;  

    public boolean isStatus() {  
        return status;  
    }  

    public void setStatus(boolean status) {  
        this.status = status;  
    }  

    public int getTotal() {  
        return total;  
    }  

    public void setTotal(int total) {  
        this.total = total;  
    }  

    public List<Cook> getList() {  
        return list;  
    }  

    public void setList(List<Cook> list) {  
        this.list = list;  
    }  
}  

建立数据加载适配器

MyAdapter.java

package com.administrator.picassodemo;  

import android.content.Context;  
import android.support.annotation.Nullable;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.BaseAdapter;  
import android.widget.ImageView;  
import android.widget.TextView;  

import com.squareup.picasso.Picasso;  

import java.util.Collection;  
import java.util.List;  

/** 
 * Created by Administrator on 2018/5/16. 
 */  

public class MyAdapter extends BaseAdapter {  
    private Context context;  
    private List<Cook> list;  

    public MyAdapter(Context context, List<Cook> 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 list.get(position).getId();  
    }  

    @Override  
    public View getView(int position, View convertView, ViewGroup parent) {  
        if(convertView == null){  
            convertView = LayoutInflater.from(context)  
                    .inflate(R.layout.item,parent,false);  
        }  
        ViewHolder holder = (ViewHolder)convertView.getTag();  
        Cook cook = list.get(position);  
        holder.title.setText(cook.getName());  
        holder.info.setText(cook.getDescription());  
        Picasso.with(context).load("http://tnfs.tngou.net/img"+cook.getImg()).into(holder.image);  
        return convertView;  
    }  

    public void addAll(Collection<? extends Cook> collection){  
        list.addAll(collection);  
        notifyDataSetChanged();  
    }  
    public static class ViewHolder{  
        private  ImageView image;  
        private TextView title;  
        private TextView info;  
        public ViewHolder(View item) {  
            image = (ImageView)item.findViewById(R.id.item_image);  
            title = (TextView)item.findViewById(R.id.item_title);  
            info = (TextView)item.findViewById(R.id.item_info);  
        }  
    }  

}  

建立接口文件

Service.java

package com.administrator.picassodemo;  

import retrofit2.Call;  
import retrofit2.http.GET;  
import retrofit2.http.Path;  
import retrofit2.http.Query;  

/** 
 * Created by Administrator on 2018/5/16. 
 */  

public interface Service {  
    @GET("/api/{category}/list")//大括号内传参数,如果连接变化只存在一个参数区别,则改变传入参数即可  
    Call<Tngou> getList(@Path("category") String category,@Query("id") int id, @Query("page") int page, @Query("rows")  int rows);  
}  

MainActivity.java

package com.administrator.picassodemo;  

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

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

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

public class MainActivity extends AppCompatActivity implements Callback<Tngou> {  


    private MyAdapter adapter;  
    private TextView mTextView;  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        mTextView = findViewById(R.id.tvContent);  
        //addConverterFactory()解析下载数据  
        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://www.tngou.net")  
                .addConverterFactory(GsonConverterFactory.create())  
                .build();  
        Service service = retrofit.create(Service.class);  
        Call<Tngou> call = service.getList("cook",0,1,20);  
//         Call<Tngou> call = service.getList("top",0,1,20);  
        call.enqueue(this);  

        ListView listView = (ListView)findViewById(R.id.main_list);  
        adapter = new MyAdapter(this,new ArrayList<Cook>());  
        listView.setAdapter(adapter);  
    }  


    @Override  
    public void onResponse(Call<Tngou> call, Response<Tngou> response) {  
        List<Cook> list = response.body().getList();  
    }  

    @Override  
    public void onFailure(Call<Tngou> call, Throwable t) {  
        t.printStackTrace();  
    }  
}  

二、POST请求xml解析

添加依赖

dependencies {  
    compile 'com.squareup.retrofit2:retrofit:2.0.2'  
    compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'  
    compile('com.squareup.retrofit2:converter-simplexml:2.0.0-beta4') {  
        exclude group: 'xpp3', module: 'xpp3'  
        exclude group: 'stax', module: 'stax-api'  
        exclude group: 'stax', module: 'stax'  
    }  
}  

AndroidManifest.xml中添加网络访问权限

<uses-permission android:name="android.permission.INTERNET"/>  

Channel.java

package com.administrator.xmldemo;  

import org.simpleframework.xml.Element;  
import org.simpleframework.xml.ElementList;  
import org.simpleframework.xml.Path;  
import org.simpleframework.xml.Root;  

import java.util.List;  

/** 
 * Created by Administrator on 2018/5/17. 
 */  
@Root(strict = false)//是否严格检查  
public class Channel {  
    @Path("channel")  
    @Element(name = "title")  
    private String title;  
    @Path("channel")  
    @ElementList(entry = "item",inline = true)  
    private List<item> list;  


    public String getTitle() {  
        return title;  
    }  

    public void setTitle(String title) {  
        this.title = title;  
    }  

    public List<item> getList() {  
        return list;  
    }  

    public void setList(List<item> list) {  
        this.list = list;  
    }  
}  

Item.java

package com.administrator.xmldemo;  


import org.simpleframework.xml.Element;  
import org.simpleframework.xml.Root;  

/** 
 * Created by Administrator on 2018/5/17. 
 */  
@Root(strict = false)  
public class Item {  
    @Element(name = "title")  
    private String title;  
}  

Service.java

package com.administrator.xmldemo;  

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

/** 
 * Created by Administrator on 2018/5/17. 
 */  

public interface Service {  
    @GET("/protal.php?mod=rss&catid=")  
    Call<Channel> getChannel();  
}  

MainActivity.java

package com.administrator.xmldemo;  

import android.support.v7.app.AppCompatActivity;  
import android.os.Bundle;  
import android.util.Log;  

import retrofit2.Call;  
import retrofit2.Callback;  
import retrofit2.Response;  
import retrofit2.Retrofit;  
import retrofit2.converter.simplexml.SimpleXmlConverterFactory;  

public class MainActivity extends AppCompatActivity implements Callback<Channel> {  

    private static final String TAG = "MainActivity";  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://www.inexus.co")  
                .addConverterFactory(SimpleXmlConverterFactory.create()).build();  
        Service service = retrofit.create(Service.class);  
        Call<Channel> channel = service.getChannel();  
        channel.enqueue(this);  
    }  

    @Override  
    public void onResponse(Call<Channel> call, Response<Channel> response) {  
                Channel channel = response.body();  
        Log.e(TAG,String.valueOf(response.body()));  
    }  

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

    }  
}