当前位置: 代码迷 >> 综合 >> Retrofit 网络请求
  详细解决方案

Retrofit 网络请求

热度:38   发布时间:2023-12-29 10:36:05.0

先看效果:

1.bean类:

package com.example.administrator.retrofit;
import java.util.List;
public class Cook {private String message;private String nu;private String ischeck;private String com;private String status;private String condition;private String state;public String getMessage() {return message;}public void setMessage(String message) {this.message = message;}public String getNu() {return nu;}public void setNu(String nu) {this.nu = nu;}public String getIscheck() {return ischeck;}public void setIscheck(String ischeck) {this.ischeck = ischeck;}public String getCom() {return com;}public void setCom(String com) {this.com = com;}public String getStatus() {return status;}public void setStatus(String status) {this.status = status;}public String getCondition() {return condition;}public void setCondition(String condition) {this.condition = condition;}public String getState() {return state;}public void setState(String state) {this.state = state;}public List<DataBean> getData() {return data;}public void setData(List<DataBean> data) {this.data = data;}private List<DataBean> data;public  class DataBean {public String getTime() {return time;}public void setTime(String time) {this.time = time;}public String getContext() {return context;}public void setContext(String context) {this.context = context;}public String getFtime() {return ftime;}public void setFtime(String ftime) {this.ftime = ftime;}private String time;private String context;private String ftime;}
}
2.Service接口:
package com.example.administrator.retrofit;import retrofit.Call;
import retrofit.http.POST;
import retrofit.http.Query;
public interface Service {@POST("query")Call<Cook> search(@Query("type") String type, @Query("postid") String postid);
}
3.MainActivity:
package com.example.administrator.retrofit;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.List;
import retrofit.Call;
import retrofit.Callback;
import retrofit.GsonConverterFactory;
import retrofit.Response;
import retrofit.Retrofit;public class MainActivity extends AppCompatActivity implements Callback<Cook> {private ListView lv;@Override
    protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);
//        创建Retrofit实例
        Retrofit retrofit = new Retrofit.Builder().baseUrl("http://www.kuaidi100.com/")//设置URL
                .addConverterFactory(GsonConverterFactory.create()).build();
//        创建接口,通过接口获取到网络接口服务
        Service service = retrofit.create(Service.class);
//        获取到接口的Call
        Call<Cook> call = service.search("yuantong", "500379523313");
//        请求数据
        call.enqueue(this);lv = (ListView) findViewById(R.id.json_lv);}@Override
    public void onResponse(Response<Cook> response, Retrofit retrofit) {List<Cook.DataBean> data = response.body().getData();lv.setAdapter(new MyAdapter(MainActivity.this, data));}@Override
    public void onFailure(Throwable t) {}
}
4.适配器:
package com.example.administrator.retrofit;import android.content.Context;
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 java.util.List;

public class MyAdapter extends BaseAdapter {private Context context;private List<Cook.DataBean> list;public MyAdapter(Context context, List<Cook.DataBean> list) {this.context = context;this.list = list;}@Override
    public int getCount() {if (list != null) {return list.size();}return 0;}@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) {if (convertView == null) {convertView = LayoutInflater.from(context).inflate(R.layout.item, parent, false);convertView.setTag(new ViewHolder(convertView));}ViewHolder holder = (ViewHolder) convertView.getTag();Cook.DataBean dataBean = list.get(position);holder.tv_title.setText(dataBean.getContext());holder.tv_info.setText(dataBean.getFtime());//使用同样开发团队的Picasso支持包进行图片加载,由于接口中返回的img路径不是全的,所以需要加上网站前缀
//        Picasso.with(context).load("http://tnfs.tngou.net/img" + cook.getData().get(position)).into(holder.iv);
        return convertView;}public static class ViewHolder {private final ImageView iv;private final TextView tv_title;private final TextView tv_info;public ViewHolder(View item) {iv = ((ImageView) item.findViewById(R.id.item_iv));tv_title = ((TextView) item.findViewById(R.id.item_title));tv_info = ((TextView) item.findViewById(R.id.item_info));}}
}
5.布局i:
activity_main:
<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.example.administrator.retrofit.MainActivity"><ListView
        android:id="@+id/json_lv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</LinearLayout>
item:
<?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="wrap_content"><ImageView
        android:id="@+id/item_iv"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@mipmap/ic_launcher" /><LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:orientation="vertical"><TextView
            android:id="@+id/item_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="标题"
            android:textAppearance="@android:style/TextAppearance.Large" /><TextView
            android:id="@+id/item_info"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="2"
            android:text="abcabcacbacbacbacbacbacacacacacasdadasd" /></LinearLayout>
</LinearLayout>


  相关解决方案