Retrofit這東西我就不多做解釋了,反正最近應用很廣,基本都快和OkHttp一起成為安卓的事實網絡訪問標准框架了。
這么好的一個東西,官網文檔實在是不算太好,說的不太清晰。按官網的經常會有“Could not locate ResponseBody converter for
”問題。
反正折騰了一番,終於跑出來了一個例子。這里把正確的例子寫出來,方便大家參考。
首先要注意的是Retrofit准確的說不是Anroid 的HttpClient,而是Java的HttpClient,所以用Java工程運行Demo的代碼即可。
文中我自己搭建了一個spring 的rest服務,很簡單,就是訪問greeting/{name},會返回一個User對象,把name賦值給這個user對象。
public class User { int id; String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + '}'; } }
1,依賴引入。
這個十分重要。。。這貨升級了一下竟然改了包名,,,蛋疼。Maven,Gradle都行,Gradle的:
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3' compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3' compile 'com.google.code.gson:gson:2.3'
Maven的:
<dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>retrofit</artifactId> <version>2.0.0-beta3</version> </dependency> <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>converter-gson</artifactId> <version>2.0.0-beta3</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.3</version> </dependency>
注意看,groupId是不一樣的。如果想直接接收對象,那要引入gson和converter。不然就只能接收字符串。
2,寫接口
import retrofit2.Call; import retrofit2.http.GET; import retrofit2.http.Path; /** * Created by csonezp on 16-1-29. */ public interface GreetingServicce { @GET("/greeting/{name}") Call<User> getGreeting(@Path("name") String name); }
service里指定了訪問的rest Api地址,路徑變量,返回值。
3,構建Retrofit實例,訪問網絡。
這個自己隨便建立個Java文件,將代碼放進去就行。
import com.google.gson.Gson; import retrofit2.Call; import retrofit2.GsonConverterFactory; import retrofit2.Response; import retrofit2.Retrofit; import java.io.IOException; /** * Created by csonezp on 16-1-29. */ public class Test { public static void main(String[] args) throws IOException { Retrofit retrofit = new Retrofit.Builder().baseUrl("http://192.168.10.225:8080")
.addConverterFactory(GsonConverterFactory.create(new Gson())).build(); GreetingServicce servicce = retrofit.create(GreetingServicce.class); Call<User> call = servicce.getGreeting("ss"); Response<User> response = call.execute(); System.out.print(response.body().toString()); } }
注意紅色部分,如果想直接接收到對象,則需要添加這個。
文中是同步訪問網絡的方式,要想在Android中使用,則需單開線程或者是換成異步訪問。
現在運行你的項目就可以了。
把Spring文件也發出來吧:
import com.example.bean.User; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.*; import java.util.concurrent.atomic.AtomicLong; /** * Created by csonezp on 16-1-21. */ @RestController @EnableAutoConfiguration public class SimpleController { private final AtomicLong counter = new AtomicLong(); private static final String template = "Hello,%s!"; @RequestMapping("/") @ResponseBody String home() { return "Hello World!"; } @RequestMapping(value = "/greeting/{name}",method = RequestMethod.GET) public User greeting(@PathVariable String name) { return new User((int) counter.incrementAndGet(), String.format(template, name)); } public static void main(String[] args) throws Exception { SpringApplication.run(SimpleController.class, args); } }