Get NMEA data type through Retrofit - java

I am wondering how can I get NMEA data type. It would be best if I could achieve this with Retrofit. The IP address of this data is http://213.73.2.250:5018. It looks like this: data
I have tried this with retrofit with this code:
public void getRestDataListRetrofit(final ViewMvpTextViewLocation viewMvpTextViewLocation) throws IOException {
Call<String> result = Api.getClient().getString();
result.enqueue(new Callback<String>() {
#Override
public void onResponse(Call<String> call, Response<String> response) {
viewMvpTextViewLocation.dismissProgressDialogWithSuccess(response.body());
}
#Override
public void onFailure(Call<String> call, Throwable t) {
viewMvpTextViewLocation.dismissProgressDialogWithError(call.request().toString() + " " + t.toString());
}
});
}
where Api class is
public class Api {
public static ApiInterface getClient() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://213.73.2.250:5018/")
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit.create(ApiInterface.class);
}
}
and getString() method is
public interface ApiInterface {
#GET("/")
Call<String> getString();
}
When I click the button to receive this data I get with error: "unexpected status line: $GPGGA, (...) ". What is wrong with this code? Thanks in advance.

I did a workaround by parsing an error message that contains a text I wanted to have.

Related

How to call API using Retrofit in Android

Link : https://mobileinterview.azurewebsites.net/api/products?PageIndex=1&PageSize=10
I am trying to call this API using retrofit in android using java but it does not return any response body it shows the response message is OK and the response code is 200, but still body is null.
Below is my code :
retrofit = new Retrofit.Builder()
.baseUrl(Constant.BaseUrl)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
MyApiService services = retrofit.create(MyApiService.class);
Call<JSONArray> call = services.getAllData();
call.enqueue(new Callback<JSONArray>() {
#Override
public void onResponse(Call<JSONArray> call, Response<JSONArray> response) {
Log.i(TAG, "onResponse: "+response);
}
#Override
public void onFailure(Call<JSONArray> call, Throwable t) {
Log.i(TAG, "onFailure: "+t.getMessage());
}
});
Below is my interface class :
#GET("products?PageIndex=1&PageSize=10")
Call<JSONArray> getAllData();
your api data is xml based , you follow this topic for parsing the xml data , hopefully it will help your problem
https://codeleague.redquark.org/2018/08/parsing-xml-using-retrofit.html

whenever i acces data from network using retrofit it works fine but in 2nd time can't find data through network using retrofit

I am trying to access data from the network (data is in the form of gson and i am using WordPress rest API) using retrofit but can't access. it shows me an error like data is null looks like retrofit can't find data but everything is good... looks like code is good and i don't know how to solve this. please help me I am a new developer.. it takes my 3 days
whenever i call getRetrofit() method it works fine... but when i call getImageRetrofit() then looks like this method won't work...this method return null value as shown in the logcat :
ImageInfo: info: null
private void getRetrofit() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitArrayApi service = retrofit.create(RetrofitArrayApi.class);
Call<List<WPPost>> call = service.getPostInfo();
call.enqueue(new Callback<List<WPPost>>() {
#Override
public void onResponse(Call<List<WPPost>> call, Response<List<WPPost>> response) {
Log.e("Latest","response: "+response.body());
for (int i=0; i<response.body().size(); i++)
{
Log.e("main ","title "+response.body().get(i).getTitle().getRendered() + " " +
response.body().get(i).getId() );
String tempDate = response.body().get(i).getDate();
tempDate = tempDate.replace("T"," ");
String tempImageHref = response.body().get(i).getLinks().getWpFeaturedmedia().get(0).getHref();
Log.e("Href", "onResponse: "+tempImageHref);
String link = response.body().get(i).getLink();
Log.e("PostLink",link);
getImageRetrofit(tempImageHref);
list.add(new LatestModel(
response.body().get(i).getTitle().getRendered(),
tempDate,
tempImageHref,
LatestModel.IMAGE_TYPE,
response.body().get(i).getLink()
)
);
}
adapter.notifyDataSetChanged();
}
#Override
public void onFailure(Call<List<WPPost>> call, Throwable t) {
t.printStackTrace();
}
});
}
private void getImageRetrofit(String ImageHref) {
Log.e("getImageRetrofit","called "+ImageHref);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitArrayApi service = retrofit.create(RetrofitArrayApi.class);
Call<List<WPPostImage>> callImage = service.getImageInfo(ImageHref);
callImage.enqueue(new Callback<List<WPPostImage>>() {
#Override
public void onResponse(Call<List<WPPostImage>> call, Response<List<WPPostImage>> response) {
Log.e("ImageInfo","info: "+response.body());
}
#Override
public void onFailure(Call<List<WPPostImage>> call, Throwable t) {
Log.e("Link Failed: ",": t.printStackTrace()" );
}
});
}
here is my RetrofitArrayApi Interface.:
public interface RetrofitArrayApi {
#GET("wp-json/wp/v2/posts?per_page=4")
Call<List<WPPost>> getPostInfo();
#GET("{id}")
Call<List<WPPostImage>> getImageInfo(#Path("id") String ImageHref); }
You said at comments that temImageHref: mubashirsaddique.com/wp-json/wp/v2/media/1780 and also your base url is baseUrl = "mubashirsaddique.com". So you send a request to this address mubashirsaddique.com/mubashirsaddique.com/wp-json/wp/v2/media/1780 when call getImageInfo.
Change your getPostInfo service. It should return just id(1780 in your case) as href value and modify RetrofitArrayApi.
#GET("wp-json/wp/v2/media/{id}")
Call<List<WPPostImage>> getImageInfo(#Path("id") String ImageHref);

Android Retrofit API usage

I'm still beginner on retrofit API for android, im trying to pass a variable into my dynamic url but it adds extra characters to it these characters are "&=".
This is what the endpoint im trying to consume looks like:
https://www.example.com/api/index.php?/Playlists/getTracks/00978d67f6933af10ec8bd8045f089a4/0673CC13-476A-4786-BF27-13ADD9C44261/9392
where "9232" is the id i am trying to pass. However when i use the retrofit library this is what my generated Url looks like:
https://www.example.com/api/index.php?/Playlists/getTracks/00978d67f6933af10ec8bd8045f089a4/0673CC13-476A-4786-BF27-13ADD9C44261/&=9392
Notice the &= being attached to the Id im sending
// Method that receives id and calls the retrofit API
private void getPlaylist(String id) {
/*Create handle for the RetrofitInstance interface*/
GetPlaylistRetrofitInterface playlistService = PlaylistClientInstance.getRetrofitInstance().create(GetPlaylistRetrofitInterface.class);
Call<List<Playlists>> call = playlistService.getPlaylist(id);
Log.d("URL", "getPlaylist: " + call.request().url());
call.enqueue(new Callback<List<Playlists>>() {
#Override
public void onResponse(Call<List<Playlists>> call, Response<List<Playlists>> response) {
myProgressBar.setVisibility(View.GONE);
populatePlayList(response.body());
}
#Override
public void onFailure(Call<List<Playlists>> call, Throwable throwable) {
myProgressBar.setVisibility(View.GONE);
Log.d("onFailure", "onFailure: " + throwable);
Toast.makeText(getActivity(), throwable.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
And this is the interface where i am receiving the url id and attaching it to the endpoint
public interface GetPlaylistRetrofitInterface {
#GET("index.php?/Playlists/getTracks/00978d67f6933af10ec8bd8045f089a4/0673CC13-476A-4786-BF27-13ADD9C44261/")
Call<List<Playlists>> getPlaylist(#Query(value = "") String id);
}
I have tried using #Path in my interface
public interface GetPlaylistRetrofitInterface {
#GET("index.php?/Playlists/getTracks/00978d67f6933af10ec8bd8045f089a4/0673CC13-476A-4786-BF27-13ADD9C44261/{id}")
Call<List<Playlists>> getPlaylist(#Path("id") String id);
}
However it made my app crash with this error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.demoapp.HomeActivity}:
java.lang.IllegalArgumentException: URL query string "/Playlists/getTracks/00978d67f6933af10ec8bd8045f089a4/0673CC13-476A-4786-BF27-13ADD9C44261/{id}" must not have replace block. For dynamic query parameters use #Query.
for method GetPlaylistRetrofitInterface.getPlaylist
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Thanks in Advance
Okay so i was able to Fix the issue, I turned out i was trying to pass a query by using #Query to an endpoint which is already a query so this is how i fixed it:
This is my updated GetPlaylistInterface
public interface GetPlaylistRetrofitInterface {
#GET()
Call<List<Playlists>> getPlaylist(#Url String url);
}
This is my updated Get Playlist method
private void getPlaylist(String id) {
myProgressBar.setVisibility(View.VISIBLE);
/*Create handle for the RetrofitInstance interface*/
GetPlaylistRetrofitInterface playlistService = RetrofitClientInstance.getRetrofitInstance().create(GetPlaylistRetrofitInterface.class);
Call<List<Playlists>> call = playlistService.getPlaylist(Config.playlist_url+id);
Log.d("URL", "getPlaylist: " + call.request().url());
call.enqueue(new Callback<List<Playlists>>() {
#Override
public void onResponse(Call<List<Playlists>> call, Response<List<Playlists>> response) {
myProgressBar.setVisibility(View.GONE);
populatePlayList(response.body());
}
#Override
public void onFailure(Call<List<Playlists>> call, Throwable throwable) {
myProgressBar.setVisibility(View.GONE);
Log.d("onFailure", "onFailure: " + throwable);
Toast.makeText(getActivity(), throwable.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
And this is the config class containing the endpoints:
public class Config {
public static String playlist_url = "index.php?/Playlists/getTracks/00978d67f6933af10ec8bd8045f089a4/0673CC13-476A-4786-BF27-13ADD9C44261/";
public static String playlist_details_url = "index.php?/Tracks/get/00978d67f6933af10ec8bd8045f089a4/0673CC13-476A-4786-BF27-13ADD9C44261/";
}
You're using #Query parameter, which has this syntax by default.
What your need is to use #Path instead of #Query. Also you need to include name of parameter into your url string.
Your code will look somehow like this:
public interface GetPlaylistRetrofitInterface {
#GET("index.php?/Playlists/getTracks/00978d67f6933af10ec8bd8045f089a4/0673CC13-476A-4786-BF27-13ADD9C44261/{id}")
Call<List<Playlists>> getPlaylist(#Path("id") String id);
}
You almost Have it first You need to use Query Parameter for you query String and Path for url/uri manipulation seems like you had those mixed up now below example should work
public interface ApiServices {
#GET("index.php?/Playlists/getTracks/00978d67f6933af10ec8bd8045f089a4/0673CC13-476A-4786-BF27-13ADD9C44261/{id}")
Call<ResponseBody> getTracks(#Path("id") String id);
}
EDIT:
You need to only define Interface and call it once. Above is interface and below is how to call it from a functions like your trying to do... its weird you have index.php? are you sure that is needed.
// Method that receives id and calls the retrofit API
private void getPlaylist(String id) {
/*Create handle for the RetrofitInstance interface*/
OkHttpClient client = new OkHttpClient.Builder()
.retryOnConnectionFailure(true)
.build();
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.baseUrl("https://stepank.com/")
.build();
ApiServices service = retrofit.create(ApiServices.class);
try {
Call<ResponseBody> call = service.getTracks(id);
call.enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
int result = -1;
setRefreshActionButtonState(false);
if (response.isSuccessful()) {
///DO STUFF HERE WITH RESPONSE
}
}
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
t.printStackTrace();
}
});
}

I get data using retrofit with post request give message Internal server error

I am using retrofit library with post request, but i did not found data. Give "Internal server error" message.
API_1 : http://www.fabgrad.com/dummy_api_1/
type : POST
data : { us_id:23 }
interface -
public interface FirstApi {
public static String URl = "http://www.fabgrad.com/";
#FormUrlEncoded
#POST("dummy_api_1")
Call<Titles> getData(#Field("us_id") String id);
}
Using retrofi in main activity -
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(FirstApi.URl)
.addConverterFactory(GsonConverterFactory.create())
.build();
FirstApi categoryMenuApi = retrofit.create(FirstApi.class);
String s="23";
Call<Titles> categoryMenuCall = categoryMenuApi.getData(s);
categoryMenuCall.enqueue(new Callback<Titles>() {
#Override
public void onResponse(Call<Titles> call, Response<Titles> response) {
Titles list = response.body();
}
#Override
public void onFailure(Call<Titles> call, Throwable t) {
Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
I am new in retrofit So please help
You only have to add / at the end of your endpoint #POST("dummy_api_1")
Just like:
#POST("dummy_api_1/")

How to get string response from Retrofit2?

I am doing android, looking for a way to do a super basic http GET/POST request. I keep getting an error:
java.lang.IllegalArgumentException: Unable to create converter for class java.lang.String
Webservice:
public interface WebService {
#GET("/projects")
Call<String> jquery();
}
then in my java:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://jquery.org")
// .addConverterFactory(GsonConverterFactory.create())
.build();
WebService service = retrofit.create(WebService.class);
Call<String> signin = service.jquery();
Toast.makeText(this, signin.toString(), Toast.LENGTH_LONG).show();
I'm literally just trying to query jquery.org/projects with a GET request and return the String that it responds with. What is wrong?
If I try to implement a custom Converter (I've found a few examples online) it complains that I didn't implement the abstract method convert(F), which none of the examples do.
Thanks.
Add Retrofit2 and ScalarsConverterFactory to your Retrofit.Builder.
adapterBuilder = new Retrofit.Builder()
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create());
To use ScalarsCoverter add following dependency to your build graddle
implementation 'com.squareup.retrofit2:converter-scalars:2.9.0'
implementation 'com.squareup.retrofit2:retrofit:2.9.0' //Adding Retrofit2
For API Call use:
Call <String> *****
Android Code :
.enqueue(new Callback<String>() {
#Override
public void onResponse(Call<String> call, Response<String> response) {
Log.i("Response", response.body().toString());
//Toast.makeText()
if (response.isSuccessful()){
if (response.body() != null){
Log.i("onSuccess", response.body().toString());
}else{
Log.i("onEmptyResponse", "Returned empty response");//Toast.makeText(getContext(),"Nothing returned",Toast.LENGTH_LONG).show();
}
}
I take a look at Retrofit library and noticed that it parses response according to the type class inside Call<T>. So you have two option:
1st: create a class according to the response from the server.
2nd: get the response and handle it yourself (Not recommended Retrofit already handles it. So why do you use Retrofit as it is tailored for this job). Anyway instead of Call<String> use Call<ResponseBody> and Call<ResponseBody> signin = service.jquery(); after this put the following
call.enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Response<ResponseBody> response, Retrofit retrofit) {
// handle success
String result = response.body().string();
}
#Override
public void onFailure(Throwable t) {
// handle failure
}
});
To get the response as a String, you have to write a converter and pass it when initializing Retrofit.
Here are the steps.
Initializing retrofit.
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL_BASE)
.addConverterFactory(new ToStringConverterFactory())
.build();
return retrofit.create(serviceClass);
Converter class for converting Retrofit's ResponseBody to String
public class ToStringConverterFactory extends Converter.Factory {
private static final MediaType MEDIA_TYPE = MediaType.parse("text/plain");
#Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations,
Retrofit retrofit) {
if (String.class.equals(type)) {
return new Converter<ResponseBody, String>() {
#Override
public String convert(ResponseBody value) throws IOException
{
return value.string();
}
};
}
return null;
}
#Override
public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations,
Annotation[] methodAnnotations, Retrofit retrofit) {
if (String.class.equals(type)) {
return new Converter<String, RequestBody>() {
#Override
public RequestBody convert(String value) throws IOException {
return RequestBody.create(MEDIA_TYPE, value);
}
};
}
return null;
}
}
And after executing service.jquery();, signin will contain JSON response.
You can try the following:
build.gradle file:
dependencies {
...
compile 'com.squareup.retrofit2:retrofit:2.0.1'
...
}
WebAPIService:
#GET("/api/values")
Call<String> getValues();
Activity file:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(API_URL_BASE)
.build();
WebAPIService service = retrofit.create(WebAPIService.class);
Call<String> stringCall = service.getValues();
stringCall.enqueue(new Callback<String>() {
#Override
public void onResponse(Call<String> call, Response<String> response) {
Log.i(LOG_TAG, response.body());
}
#Override
public void onFailure(Call<String> call, Throwable t) {
Log.e(LOG_TAG, t.getMessage());
}
});
I have tested with my Web serivce (ASP.Net WebAPI):
public class ValuesController : ApiController
{
public string Get()
{
return "value1";
}
}
Android Logcat out: 04-11 15:17:05.316 23097-23097/com.example.multipartretrofit I/AsyncRetrofit2: value1
Hope it helps!
call.enqueue(new Callback<Object>() {
#Override
public void onResponse(#NonNull Call<Object> call, #NonNull Response<Object> response) {
if (response.isSuccessful()) {
Gson gson = new Gson();
String successResponse = gson.toJson(response.body());
Log.d(LOG_TAG, "successResponse: " + successResponse);
} else {
try {
if (null != response.errorBody()) {
String errorResponse = response.errorBody().string();
Log.d(LOG_TAG, "errorResponse: " + errorResponse);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
#Override
public void onFailure(#NonNull Call<Object> call, #NonNull Throwable t) {
}
});
Just use log level BODY in intercepror:
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()....
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
clientBuilder.addNetworkInterceptor(httpLoggingInterceptor);
And you can see body in logcat like:
D/OkHttp: {"blablabla":1,.... }
D/OkHttp: <-- END HTTP (1756-byte body)
This solution for debug only, not for get String directly in code.
thought it might be late to answer just use response.body()?.string() and you'll have your response as a string.
I change the Response type, instead of adding ScalarsConverterFactory like this
interface YourApiService {
#GET("/endpoint")
fun getResponse(): Call<ResponseBody>
}
And then you can get the string object by
val str = response.body().toString()
String body = new String(((TypedByteArray) response.getBody()).getBytes());
if you found
java.lang.ClassCastException: retrofit.client.UrlConnectionClient$TypedInputStream cannot be cast to retrofit.mime.TypedByteArray
then put this in your RestAdapter
.setLogLevel(RestAdapter.LogLevel.FULL)
for Example:
RestAdapter restAdapter = new RestAdapter.Builder()
.setLogLevel(RestAdapter.LogLevel.FULL)
.setEndpoint(Root_url)
.build();

Categories

Resources