1、定义接口
public interface JieKou { @GET("toutiao/index?type=top&key=097060266650f67b2cebd2a06aded587") Call<MyBean> getdata(); @GET("toutiao/index") Call<MyBean> getdata(@Query("type")String name,@Query("key")String age); @GET("toutiao/index") Call<MyBean> getdata(@QueryMap Map<String ,String> map); @FormUrlEncoded @POST("toutiao/index") Call<MyBean> postData(@Field("type")String name,@Field("key")String age); @FormUrlEncoded @POST("toutiao/index") Call<MyBean> postData(@FieldMap Map<String ,String> map); }
2、建实体MyBean
串放进实体Bean
3、XML:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" 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.mmaster.myapplication.MainActivity"> <Button android:id="@+id/but" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="GET" /> <Button android:id="@+id/but2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="GET2" /> <Button android:id="@+id/but3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="GET3" /> <Button android:id="@+id/post1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Post1" /> <Button android:id="@+id/post2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Post2" /> </LinearLayout>
4、MainActivity页面 3GET 2Post请求
package com.example.mmaster.myapplication; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import com.google.gson.Gson; import java.util.HashMap; import java.util.Map; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; public class MainActivity extends AppCompatActivity implements View.OnClickListener, Callback<MyBean>{ private Button but; private Retrofit builder; private JieKou jieKou; private Button but2; private Button but3; private Button post1; private Button post2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); builder = new Retrofit.Builder() .baseUrl("http://v.juhe.cn/") .addConverterFactory(GsonConverterFactory.create(new Gson())) .build(); jieKou = builder.create(JieKou.class); } private void initView() { but = (Button) findViewById(R.id.but); but.setOnClickListener(this); but2 = (Button) findViewById(R.id.but2); but2.setOnClickListener(this); but3 = (Button) findViewById(R.id.but3); but3.setOnClickListener(this); post1 = (Button) findViewById(R.id.post1); post1.setOnClickListener(this); post2 = (Button) findViewById(R.id.post2); post2.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.but: Call<MyBean> getdata = jieKou.getdata(); getdata.enqueue(this); break; case R.id.but2: Call<MyBean> top = jieKou.getdata("top", "097060266650f67b2cebd2a06aded587"); top.enqueue(this); break; case R.id.but3: Map<String,String> map=new HashMap<>(); map.put("type","top"); map.put("key","097060266650f67b2cebd2a06aded587"); Call<MyBean> getdata1 = jieKou.getdata(map); getdata1.enqueue(this); break; case R.id.post1: Call<MyBean> top1 = jieKou.postData("top", "097060266650f67b2cebd2a06aded587"); top1.enqueue(this); break; case R.id.post2: Map<String,String> map1=new HashMap<>(); map1.put("type","top"); map1.put("key","097060266650f67b2cebd2a06aded587"); Call<MyBean> myBeanCall = jieKou.postData(map1); myBeanCall.enqueue(this); break; } } @Override public void onResponse(Call<MyBean> call, Response<MyBean> response) { MyBean body = response.body(); Log.e("h", body.getResult().toString()); } @Override public void onFailure(Call<MyBean> call, Throwable t) { } }