大家好,我对休息服务有非常基本的了解。我已经实现了一个关于Java的基本程序来获得输出" hello world!"在URI http://localhost:8080/greeting/greet
package greeting;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
@Path("/greet")
public class Greet {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String justGreet(){
return "hello world!";
}
}
我想在Android上实现类似的类型,以便我可以在该网络中的任何浏览器上看到某些URI的输出。我混淆了哪些依赖项或库必须用于Android才能获得它。希望得到答案。
先谢谢你!
编辑:我按照建议使用了Retrofit库,它运行良好,但我看不到输出" Hello World!"在此URI http://192.168.0.104:8080/greeting/greet1,如usig Java和Jax-rs。
这是实现
package abc.xyz.com.retrotut;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
private static final String URL = "http://192.168.0.104:8080/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiService api = retrofit.create(ApiService.class);
Greeting greet = new Greeting("Hello World!");
Call<Greeting> call = api.getGreeting(greet);
call.enqueue(new Callback<Greeting>() {
@Override
public void onResponse(Call<Greeting> call, Response<Greeting> response) {
Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<Greeting> call, Throwable t) {
Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_SHORT).show();
}
});
}
}
Pojo类
public class Greeting {
private String greet;
public Greeting(String greet) {
this.greet = greet;
}
}
接口
public interface ApiService {
@POST("greeting/greet1")
Call<Greeting> getGreeting(@Body Greeting greet);
}
然而,我正在获得成功&#34;,在执行应用程序时,我无法获得我想要的输出。我应该使用哪些库或任何替代品?