I am new to Android and I want to make an application based on REST API.
So I search and found the Retrofit 2.
I used a sample of retrofit that I found in Github to learn how it works.
This files are my Java classes :
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textv = findViewById(R.id.textv);
GitHubClient gitHubClient;
gitHubClient = ServiceGenerator.createService(GitHubClient.class);
final Call<GitHubUser> call = gitHubClient.getFeed("google");
call.enqueue(new Callback<GitHubUser>() {
#Override
public void onResponse(Call<GitHubUser> call, Response<GitHubUser> response) {
GitHubUser gitModel = response.body();
if (gitModel != null) {
textv.setText(getString(R.string.main_response_text,
gitModel.getName(),
gitModel.getBlog()));
} else {
textv.setText("user doesn't exist");
}
}
#Override
public void onFailure(Call<GitHubUser> call, Throwable t) {
textv.setText(t.getMessage());
}
});
}
}
GithubClient.java
public interface GitHubClient {
#GET("users/{user}")
Call<GitHubUser> getFeed(#Path("user") String user);
}
ServiceGenerator.java
public class ServiceGenerator {
public static final String API_BASE_URL = "https://api.github.com/";
private static HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
private static Interceptor logging = interceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
private static OkHttpClient httpClient = new OkHttpClient.Builder()
.addInterceptor(logging)
.build();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create());
public static <S> S createService(Class<S> serviceClass) {
Retrofit retrofit = builder.client(httpClient).build();
return retrofit.create(serviceClass);
}
}
GithubUser.java
public class GitHubUser {
private String name;
private String blog;
public String getName() {
return name;
}
public String getBlog() {
return blog;
}
}
JSON code from Github
{
...
"name": "Google",
"blog": "https://opensource.google.com/",
...
}
I want to get the name and blog URL
And when I run the program the output is:
javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xb8952b10: Failure in SSL library, usually a protocol error
error:1407742E:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert protocol version(external/openssl/ssl/s23_clnt.c:741 0x83bc3770:0x00000000)
What's the problem and how can I fix?
The first step here would be to understand the error.
Try printing the error from your onFailure method like this:
#Override
public void onFailure(Call<GitHubUser> call, Throwable t) {
textv.setText("unknown error"); // can also do textv.setText(t.getMessage()) to display error reason
t.printStackTrace();
}
You should get in your logcat some more useful information about what went wrong, it will be easier to figure out how to solve it after that
Also have you added internet access permission in your manifest?
SSL Exception comes when SSL Certificate issue . You need to add ssl certificate to server side.
Have you tried
android:networkSecurityConfig="#xml/network_security_config"
in your manifest tag
also put the following file in res >> xml >>
network_security_config.xml
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">static.food2fork.com</domain>
</domain-config>
</network-security-config>
Related
Hello i didn't found solution for my problem. In new OkHttp and Retrofit is some function:
HandshakeCertificates certificates = new HandshakeCertificates.Builder()
.addPlatformTrustedCertificates()
.addInsecureHost("192.168.0.150")
.build();
I am trying to connect my Android App to Spring Boot Server. This Server must use HTTPS - not my idea.
On this server i generated self-sign certyficate, but still i got error, now i don't have any ideas. Here is a full error:
java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
Below i insert code of Retrofit generator:
public class ServiceGenerator {
public static final String API_BASE_URL = "https://192.168.0.150:8443/";
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
// private static OkHttpClient httpClient = getUnsafeOkHttpClient();
private static Retrofit.Builder builder =
new Retrofit.Builder()
.baseUrl(API_BASE_URL)
.addConverterFactory(GsonConverterFactory.create());
private static Retrofit retrofit = builder.build();
public static <S> S createService(Class<S> serviceClass) {
return createService(serviceClass, null);
}
static public <S> S createService(
Class<S> serviceClass, final String authToken) {
if (!TextUtils.isEmpty(authToken)) {
AuthenticationInterceptor interceptor =
new AuthenticationInterceptor(authToken);
if (!httpClient.interceptors().contains(interceptor)) {
httpClient.addInterceptor(interceptor);
HandshakeCertificates certificates = new HandshakeCertificates.Builder()
.addPlatformTrustedCertificates()
.addInsecureHost("192.168.0.150")
.build();
httpClient.sslSocketFactory(certificates.sslSocketFactory(), certificates.trustManager());
OkHttpClient okHttpClient = httpClient.build();
builder.client(okHttpClient);
retrofit = builder.build();
}
}
return retrofit.create(serviceClass);
}
}
Request Code:
private void doLoginRequest() {
DeviceAPI deviceAPI = ServiceGenerator.createService(DeviceAPI.class);
Call<JWTResponse> call = deviceAPI.login(new Login(usernameEditText.getText().toString(), passwordEditText.getText().toString()));
call.enqueue(new Callback<JWTResponse>() {
#Override
public void onResponse(Call<JWTResponse> call, Response<JWTResponse> response) {
if (response.isSuccessful()) {
Toast.makeText(LoginActivity.this, response.body().toString(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(LoginActivity.this, response.message(), Toast.LENGTH_SHORT).show();
}
}
#Override
public void onFailure(Call<JWTResponse> call, Throwable t) {
System.out.println(t.getCause());
Toast.makeText(LoginActivity.this, t.getMessage() , Toast.LENGTH_SHORT).show();
}
});
}
Api Code:
public interface DeviceAPI {
#POST("api/auth/signin")
Call<JWTResponse> login(#Body Login login);
}
If you need more information give me feedback!
This example shows connecting twice to a host - Once with a valid HTTPS Handshake, and second using addInsecureHost. n.b. you won't get a handshake peer in the second because the handshake won't result in valid certificates.
The same will apply if you run against a dev server, so edit the example to use your devserver.
https://gist.github.com/yschimke/796e58a6152137bdcd7d2f9d63e26363
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.tls.HandshakeCertificates.Builder
fun main() {
val request = Request.Builder()
.url("https://httpbin.org/get")
.build()
var client = OkHttpClient();
var response = client.newCall(request).execute()
println(response.handshake?.peerPrincipal) // CN=httpbin.org
println(response.code)
val certificates = Builder()
.addInsecureHost("httpbin.org")
.build()
client = OkHttpClient.Builder().sslSocketFactory(certificates.sslSocketFactory(),
certificates.trustManager
).build();
response = client.newCall(request).execute()
println(response.handshake?.peerPrincipal) // null
println(response.code)
}
I know there are a lot of threads regarding this and i did go through them and also looked into the same Retrofit POST example,but i'm not sure what am i doing wrong in this,lemme know if any
#Multipart
#POST("api/customerdetail")
Call<Array> addUser(#Part("CustomerName") String CustomerName, #Part("CustomerId") String CustomerId, #Part("UserId") String UserId, #Part("VehicleCompanyName") String VehicleCompanyName, #Part("VehicleModelType")String VehicleModelType, #Part("VehicleNumber")String VehicleNumber, #Part("Location")String Location);
//METHOD USED TO CALL
private void simpleMethod() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://endpoint.net/")
.addConverterFactory(GsonConverterFactory.create())
.build();
GetDataService service = retrofit.create(GetDataService.class);
Call<Array> arrayListCall = service.addUser("Peter Jenkins", UUID.randomUUID().toString(),"user2","AUDI","R3","BVZ-009","-55,-93.23"); arrayListCall.enqueue(new Callback<Array>() {
#Override
public void onResponse(Call<Array> call, Response<Array> response) {
Log.e("RESPONSE",response.toString());
}
#Override
public void onFailure(Call<Array> call, Throwable t) {
Log.e("ERROR",t.toString());
} }); }
Works like a Charm in Postman,and uploading an image is not necessary,atleast from the api end
Any inputs would be deeply appreciated
I saw this issue many times, but still can't understand. It looks like I send request on site and it's body isn't correct. But why? May be I don't understand clearly how retrofit works, but do not I just collect a link for the request and wait for the answer from the server?
Here is the link: here
An interface with request
public interface NService {
#GET("/computers?p=2")
Call<Model> getItems();
}
and class with base URL
public class APIUtils {
public static final String BASE_URL = "http://testwork.nsd.naumen.ru/rest/";
public static NService getMService() {
return RetrofitClient.getClient(BASE_URL).create(NService.class);
}
}
retrofit build class
public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(String baseURL) {
if (retrofit==null) {
Gson gson = new GsonBuilder()
.setLenient()
.create();
retrofit = new Retrofit.Builder()
.baseUrl(baseURL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
}
JSON were parsed though this
and MainActivity is:
public class MainActivity extends AppCompatActivity {
private List<Item> responseModel;
private NService mService;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d("testcrap", "before loading set");
setContentView(R.layout.activity_main);
mService = APIUtils.getMService(); // Строим retrofit объект, собираем ссылку
Log.d("testcrap", "before loading");
loadSomeCrap();
Log.d("testcrap", "after loading");
}
public void loadSomeCrap() {
Log.d("testcrap", "started parsing");
mService.getItems().enqueue(new Callback<Model>() {
#Override
public void onResponse(Call<Model> call, Response<Model> response) {
Log.d("testcrap", "started onResponse");
if(response.isSuccessful()) {
Log.d("testcrap", "posts loaded from API");
}else {
Log.d("testcrap", "posts not loaded from API");
}
}
#Override
public void onFailure(Call<Model> call, Throwable t) {
//showErrorMes sage();
Log.d("testcrap", t.toString());
}
});
}
}
Log:
before loading set
before loading
started parsing
after loading
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 13 column 1 path $
So, if it's wrong request, what should I change for normal work?
Make these changes ..
BASE_URL = "http://testwork.nsd.naumen.ru/"
And then on interface ..
#GET("rest/computers?p=2")
.............
Your API or JSON response is returning JSON_OBJECT but you are getting it in String so this JsonSyntaxException is shown you. Try to fetch response in OBJECT.
Sorry for my English. I want use this service. For determine the language of the text.
Request(Curl):
curl -X POST -d "outputMode=json" --data-urlencode text#ibm.txt -d "url=http://www.ibm.com/us-en/" "https://gateway-a.watsonplatform.net/calls/text/TextGetLanguage?apikey=%API_KEY%"
I use Retrofit for request.
public interface LanguageDetectionApi {
public static final String ROOT_URL = "https://gateway-a.watsonplatform.net/calls/";
#POST("/text/TextGetLanguage")
Call<List<PostModel>> getData(#Query("apikey") String apikey, #Query("text") String text);
}
Create retrofit object:
public class App extends Application {
private static LanguageDetectionApi _languageDetectionApi;
private Retrofit _retrofit;
#Override
public void onCreate() {
super.onCreate();
_retrofit = new Retrofit.Builder()
.baseUrl(_languageDetectionApi.ROOT_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
_languageDetectionApi = _retrofit.create(LanguageDetectionApi.class);
}
public static LanguageDetectionApi getLanguageDetectionApi() {
return _languageDetectionApi;
}
}
And send request:
App app = new App();
app.onCreate();
app.getLanguageDetectionApi().getData("4978e60252ae102dfe1341146bb8cc3ec4bbbd78", textForRecognition).enqueue(new Callback<List<PostModel>>() {
#Override
public void onResponse(Call<List<PostModel>> call, Response<List<PostModel>> response) {
List<PostModel> posts = new ArrayList<>();
posts.addAll(response.body());
}
#Override
public void onFailure(Call<List<PostModel>> call, Throwable t) {
Toast.makeText(MainActivity.this, "An error occurred during networking", Toast.LENGTH_SHORT).show();
}
});
PostModel i generated in site http://www.jsonschema2pojo.org/.
Questions:
No response comes to me, although apikey are exactly valid.
How to specify in the interface parametr "outputMode=json"?
And I translated correctly cURL to LanguageDetectionApi?
It seems to me that the whole mistake in the class LanguageDetectionApi. Can you help deal with this? Thank you!
change url code like below:
public interface LanguageDetectionApi {
public static final String ROOT_URL = "https://gateway-a.watsonplatform.net";
#POST("/calls/text/TextGetLanguage")
Call<List<PostModel>> getData(#Query("apikey") String apikey, #Query("text") String text);
}
base url should be ony host name.
Can anyone please give me some example how we can use protobuf in retrofit - I tried but its failed with some error , let me give you a sample of my implementation on that.
I hope you guys will help me.
ApiInterface.java
public interface ApiInterface {
#GET
Call<CommonProto.Country> makeGetRequest(#Url String url);
}
ApiClient.java
public class ApiClient {
public static final String BASE_URL = "**************************";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(Proto3ConverterFactory.create())
.build();
}
return retrofit;
}
}
MainActivity.java
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
Call<CommonProto.Country> call = apiService.makeGetRequest("Services/CountryServices/GetAllCountry");
call.enqueue(new Callback<CommonProto.Country>() {
#Override
public void onResponse(Call<CommonProto.Country> call, Response<CommonProto.Country> response) {
String bodyString = null;
try {
Log.e("RETROFIT ::::::: ", String.valueOf(response.body())+"TEST");
} catch (Exception e) {
Log.e("RETROFIT ERROR ::::::: ", e.getMessage()+"TEST");
e.printStackTrace();
}
}
#Override
public void onFailure(Call<CommonProto.Country> call, Throwable t) {
// Log error here since request failed
Log.e(TAG, t.toString());
}
}
);
when i run this way i got the error
java.lang.RuntimeException: com.google.protobuf.InvalidProtocolBufferException: Protocol message tag had invalid wire type.
my Proto.java file and also have Proto.proto file both are here in this link,
https://drive.google.com/folderview?id=0B4loQuzINvHCRUlNbk5LUXE1NXM&usp=sharing
Please let me know how to do this GET Req and also I was Struggling with POST Req.
you can create interface like this
public interface LoginInterface {
#FormUrlEncoded
#POST("url goes here")
Call<LoginResponseData> getUserLoginDeatail(#FieldMap Map<String, String> fields);
}
make an instance of retro file and call interface method something like this
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("base url")
.build();
webApis = retrofit.create(WebApis.class);
Call<LoginResponseData> call = webApis.getCurrentRide(keyValue);
call.enqueue(new Callback<LoginResponseData>() {
#Override
public void onResponse(Call<LoginResponseData> call, Response<LoginResponseData> response) {
try {
} catch (Exception e) {
// customizedToast.showToast(context.getResources().getString(
// R.string.exception));
e.printStackTrace();
}
}
#Override
public void onFailure(Call<LoginResponseData> call, Throwable t) {
}
});
for protocol buffer you can find a reference here