Skip to content

Instantly share code, notes, and snippets.

@akexorcist
Created January 5, 2016 18:30
Show Gist options
  • Save akexorcist/d0c730d297691f840da7 to your computer and use it in GitHub Desktop.
Save akexorcist/d0c730d297691f840da7 to your computer and use it in GitHub Desktop.
The answer of someone's question in android developer facebook group https://goo.gl/nA1Vnz
01-06 01:24:42.095 11422-11422/com.akexorcist.myapplication E/Check: onResponse
01-06 01:24:42.095 11422-11422/com.akexorcist.myapplication E/Check: Message : มีปัญหาอะไร?
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ServiceConnection.getService().getData().enqueue(new Callback<ResponseData>() {
@Override
public void onResponse(Response<ResponseData> response, Retrofit retrofit) {
Log.e("Check", "onResponse");
Log.e("Check", "Message : " + response.body().getMessage());
}
@Override
public void onFailure(Throwable t) {
Log.e("Check", "onFailure");
}
});
}
}
public interface MyServiceInterface {
@POST("your_target_url")
Call<ResponseData> getData();
}
public class MyServiceManager {
private static Retrofit retrofit;
private static Retrofit getServiceInstance() {
if(retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl("your_base_url")
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
public static MyServiceInterface getService() {
return getServiceInstance().create(MyServiceInterface.class);
}
}
public class ResponseData {
String status;
String message;
public String getStatus() {
return status;
}
public String getMessage() {
return message;
}
}
status : 200
Connection : keep-alive
Content-Length : 63
Content-Type : application/json; charset=utf-8
Date : Tue, 05 Jan 2016 18:13:28 GMT
ETag : W/"3f-4db558be"
X-Powered-By : Express
{"status":"ok","message":"มีปัญหาอะไร?"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment