Reputation: 304
Requirement->
To get list of Objects Using GET Request:
passing Request headers:
X-ACCESS-TOKEN = Token received after successful sign in
X-USER-EMAIL = Email used in login.
I am using this code to login->
private void normalLoginToServer() {
final ProgressDialog progressDialog = GeneralUtil.createProgressDialog(this, "Logging into app..");
progressDialog.show();
Instead of using JSONObject, i need to pass Request Headers.
in a below Astrike code. how to pass Headers.? please help me.
***JSONObject outer_body = new JSONObject();
JSONObject body = new JSONObject();
try {
body.put(Constants.USER_EMAIL, _emailText.getText().toString().trim());
body.put(Constants.USER_PWD, _passwordText.getText().toString().trim());
outer_body.put(Constants.USER, body);***
try {
TypedInput typedInput = new TypedByteArray("text/plain", outer_body.toString().getBytes("UTF-8"));
apiService.loginToServer(typedInput, new Callback<UserInfo>() {
@Override
public void success(UserInfo response, Response response2) {
int status = response2.getStatus();
switch (status) {
case 200:
if (progressDialog.isShowing())
progressDialog.dismiss();
if (response == null)
return;
String status1 = response.getStatus();
if (status1.equalsIgnoreCase("success")) {
Toast.makeText(context, "Successful login", Toast.LENGTH_SHORT).show();
Data data = response.getData();
User user = data.getUser();
String token = user.getAccess_token();
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra(Constants.ACCESS_TOKEN, token);
startActivity(intent);
finish();
} else if (status1.contains("failure")) {
Toast.makeText(context, "Username not found", Toast.LENGTH_SHORT).show();
}
break;
case 500:
Toast.makeText(context, getResources().getString(R.string.server_error), Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public void failure(RetrofitError retrofitError) {
if (progressDialog.isShowing())
progressDialog.dismiss();
if (retrofitError != null) {
if (retrofitError.getKind() != null) {
if (retrofitError.getKind().equals(RetrofitError.Kind.NETWORK)) {
Toast.makeText(context, "Check your network connection and try again later",
Toast.LENGTH_SHORT).show();
}
} else if (retrofitError.getResponse() != null) {
if (retrofitError.getResponse().getStatus() == 500) {
Toast.makeText(context, getResources().getString(R.string.server_error), Toast.LENGTH_SHORT).show();
}
}
}
}
});
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
Thanks in Advenced.!!
Upvotes: 0
Views: 497
Reputation: 894
Assuming you want to pass static headers(those who won't change for a individual requests). for example this code adds cache control header to /tasks request
public interface UserService {
@Headers("Cache-Control: max-age=640000")
@GET("/tasks")
List<Task> getTasks();
}
and if you need to pass dynamic headers (those changing on individual requests)
public interface UserService {
@GET("/tasks")
List<Task> getTasks(@Header("Content-Range") String contentRange);
}
More on Retrofit Add Custom Request Header
Upvotes: 1
Reputation: 10330
You can use @Header
in your retrofit api interface. For example, something like:
void loginToServer(@Header("your_header") String yourHeaderValue, Callback<UserInfo> callback);
Upvotes: 1