This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NetworkConnectionManager { | |
public NetworkConnectionManager() { | |
} | |
public void callServer(final OnNetworkCallbackListener listener,String username){ | |
Retrofit retrofit = new Retrofit.Builder() | |
.baseUrl("https://api.github.com") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Retrofit retrofit = new Retrofit.Builder() | |
.baseUrl("https://api.github.com") | |
.addConverterFactory(GsonConverterFactory.create()) | |
.build(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void onResponse(Response<User> response, Retrofit retrofit) { | |
User user = response.body(); | |
if (user == null) { | |
//404 or the response cannot be converted to User. | |
ResponseBody responseBody = response.errorBody(); | |
if (responseBody != null) { | |
listener.onBodyError(responseBody); | |
} else { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
call.enqueue(new Callback<User>() { | |
@Override | |
public void onResponse(Response<User> response, Retrofit retrofit) { | |
} | |
@Override | |
public void onFailure(Throwable t) { | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Call call = git.getUser(username); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void onResponse(User user, Retrofit retrofit) { | |
//200 | |
String data = "Github Name :" + user.getName() + | |
"\nBlog :"+user.getฺBlog() + | |
"\nCompany Name :" + user.getCompany(); | |
tvResult.setText(data); | |
//แสดง layout result | |
layoutForm.setVisibility(View.GONE); | |
layoutResult.setVisibility(View.VISIBLE); | |
layoutProgress.setVisibility(View.GONE); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OnNetworkCallbackListener networkCallbackListener = new OnNetworkCallbackListener() { | |
@Override | |
public void onResponse(User user, Retrofit retrofit) { | |
//200 | |
} | |
@Override | |
public void onBodyError(ResponseBody responseBodyError) { | |
//404 (error not null) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
View.OnClickListener onBtSendClickListener = new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
} | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void onClick(View v) { | |
//แสดง layout progress (ควรแยกไปอีก method ชื่อว่า showLoading()) | |
layoutForm.setVisibility(View.GONE); | |
layoutResult.setVisibility(View.GONE); | |
layoutProgress.setVisibility(View.VISIBLE); | |
new NetworkConnectionManager().callServer(networkCallbackListener, edUsername.getText().toString()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private LinearLayout layoutForm; | |
private LinearLayout layoutProgress; | |
private LinearLayout layoutResult; | |
private EditText edUsername; | |
private Button btSend; | |
private TextView tvResult; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
setContentView(); |