I have to consume a rest service that they have given me but it asks me for a username and password and I don't know how to enter it in the request.
I tried with retrofit2 to connect and show some data in a listview and I have no problems, but the rest service that they have given me to show it later in a listWiew asks me for a username and password (I have them) but I don't know how to pass it to the application.
It hangs up when you make the request without giving me the option to enter the username and password, should I configure it in the app? Should I use any service?
The rest service tells me this
The thing is that when I call get to get the data from the rest service, the application crashes and I don't know why I should pass the username and password, but I don't know how.
The format of the Json that the rest service gives me is this:
{"result":[[[12,"01",1,"Power failure",0,1],[12,"01",2,"Max level (activation)",0, 0],[12,"01",3,"Max level (off)",0,1]]]}
The mainActivity is this
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import com.example.lista.R;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class MainActivity extends AppCompatActivity {
ListView list;
ArrayList<String> titles = new ArrayList<>();
ArrayAdapter arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, titles);
list = findViewById(R.id.list);
list.setAdapter(arrayAdapter);
getPosts();
}
private void getPosts() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://jsonplaceholder.typicode.com")
.addConverterFactory(GsonConverterFactory.create())
.build();
PostService postService = retrofit.create(PostService.class);
Call<List<Post>> call = postService.getPost();
call.enqueue(new Callback<List<Post>>() {
@Override
public void onResponse(Call<List<Post>> call, Response<List<Post>>
response) {
if(response == null)
{
Log.d("MainActivity", "Response is null");
}else
{
Log.d("MainActivity", "Response has contents");
}
for (Post post : response.body()) {
titles.add(post.getTitle());
}
arrayAdapter.notifyDataSetChanged();
}
@Override
public void onFailure(Call<List<Post>> call, Throwable t) {
Log.d("MainActivity", String.valueOf(titles.add(t.getMessage())));
}
});
}
}
And the postService like this
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Header;
public interface PostService {
String API_ROUTE =" /posts";
@GET(API_ROUTE)
Call< List<Post> > getPost();
}
The type of authentication requested by the server is Basic Auth. Credentials must be sent in the header using Authorization. The credentials must be separated username and password by the character ":". Example: myuser:mypassword. Additionally they must be base64 encoded.
An example could be the following:
In PostService:
In MainActivity: