0

I am working on android studio. I am trying to consume Rest API. Right now I am using Postman for doing my work.

Body Part

enter image description here

Header Part

enter image description here

Response

{
"data": [
    {
        "global_device_id": "982010",
        "msn": "002998002010",
        "relay_operate": "1"
    }
],
"message": "Auxiliary Relay Status",
"transactionid": "Abdfqwe332432423ti", 
"status": "1"
} 

What I have done?

@POST("UIP/on_demand_parameter_read")
Call<UIPResponse> getRelayResponse(@Header("transactionid") String tid, @Header("privatekey") String pk, @Header("Content-Type") String ct,
                              @Body OnDemand onDemand);

OnDemand class

public class OnDemand {

@SerializedName("global_device_id")
@Expose
private String global_device_id;
@SerializedName("type")
@Expose
private String type;



public String getGlobal_device_id() {
    return global_device_id;
}

public void setGlobal_device_id(String global_device_id) {
    this.global_device_id = global_device_id;
}


public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}


public OnDemand(String global_device_id, String type) {
    this.global_device_id = global_device_id;
    this.type = type;
}
}

UIP Response Class

package com.thumbsol.accuratemobileassetsmanagament.model;



import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.List;


public class UIPResponse
{
@SerializedName("data")
@Expose
private List<Datum> data = null;
@SerializedName("message")
@Expose
private String message;
@SerializedName("transactionid")
@Expose
private String transactionid;
@SerializedName("status")
@Expose
private String status;

public List<Datum> getData() {
    return data;
}

public void setData(List<Datum> data) {
    this.data = data;
}

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public String getTransactionid() {
    return transactionid;
}

public void setTransactionid(String transactionid) {
    this.transactionid = transactionid;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}

}

class Datum {

@SerializedName("global_device_id")
@Expose
private String globalDeviceId;
@SerializedName("msn")
@Expose
private String msn;
@SerializedName("relay_operate")
@Expose
private String relayOperate;

public String getGlobalDeviceId() {
    return globalDeviceId;
}

public void setGlobalDeviceId(String globalDeviceId) {
    this.globalDeviceId = globalDeviceId;
}

public String getMsn() {
    return msn;
}

public void setMsn(String msn) {
    this.msn = msn;
}

public String getRelayOperate() {
    return relayOperate;
}

public void setRelayOperate(String relayOperate) {
    this.relayOperate = relayOperate;
}

}

Retrofit Class

public static Retrofit getOnDemandRequest() {
    if (retrofitSignalStrength == null) {
        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(logging)
                .connectTimeout(1000, TimeUnit.SECONDS)
                .readTimeout(1000,TimeUnit.SECONDS)
                .build();
        retrofitSignalStrength = new
                Retrofit.Builder().baseUrl(BASE_URL_STAT)
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();
    }
    return retrofitSignalStrength;
}

Then Finally In my button onClick I have done

 OnDemand onDemand = new OnDemand(global_device_id, type);

            Retrofit retrofit = RetrofitClient.getOnDemandRequest();
            RetrofitInterface retrofitInterface = retrofit.create(RetrofitInterface.class);
            Call<UIPResponse> call = retrofitInterface.getRelayResponse(transactionid, privatekey, content_type, onDemand);

            call.enqueue(new retrofit2.Callback<UIPResponse>() {
                @Override
                public void onResponse(Call<UIPResponse> call, Response<UIPResponse> response) {

                    boolean isSuccess = false;

                    if(response.isSuccessful())
                    {

                    }


                }

                @Override
                public void onFailure(Call<UIPResponse> call, Throwable t) {

                }
            });

Debugging

While debugging I am getting below log

   2020-08-12 14:06:29.551 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: --> POST    http://port:IP/program/on_demand_parameter_read
   2020-08-12 14:06:29.552    18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Content-Type: application/x-www-form-urlencoded
  2020-08-12 14:06:29.552 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Content-Length: 43
  2020-08-12 14:06:29.552 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: transactionid: Abdfqwe332432423ti
  2020-08-12 14:06:29.553 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: privatekey: 87551213232ed334ssdewi
  2020-08-12 14:06:29.555 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: {"global_device_id":"982010","type":"AUXR"}
  2020-08-12 14:06:29.556 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: --> END POST (43-byte body)
  2020-08-12 14:06:30.503 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: <-- 200 http://IP:port/program/on_demand_parameter_read (944ms)
  2020-08-12 14:06:30.504 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Transfer-Encoding: chunked
  2020-08-12 14:06:30.505 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Date: Wed, 12 Aug 2020 09:06:31 GMT
  2020-08-12 14:06:30.515 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: Failed to obtain interface by incoming parameters
  2020-08-12 14:06:30.516 18991-19117/com.thumbsol.accuratemobileassetsmanagament D/OkHttp: <-- END HTTP (49-byte body)

I must be missing something that I don't know

Any help would be highly appreciated.

6
  • Hey, just add Log.d("AndroidNetworking", "response received: " + response.toString()); to onResponse() callback and Log.d("AndroidNetworking", "error occured: " + error.toString()); to onError callback. And make sure you added Internet Permission to AndroidManifest.xml file: developer.android.com/training/basics/network-ops/connecting Commented Aug 12, 2020 at 5:47
  • Did u pass statusBody? Commented Aug 12, 2020 at 5:57
  • @navylover yes I did .addBodyParameter(statusBody) Commented Aug 12, 2020 at 5:58
  • @MariuszBrona I am still unable to see the log :( Commented Aug 12, 2020 at 5:58
  • But can your debugger access those Logs (stop at the line where the logs are)? Commented Aug 12, 2020 at 6:03

4 Answers 4

1

Use @FormUrlEncoded

@Headers("Content-Type: application/x-www-form-urlencoded")
@FormUrlEncoded
@POST("UIP/on_demand_parameter_read")
Call<UIPResponse> getRelayResponse(
    @Header("transactionid") String tid, 
    @Header("privatekey") String pk,
    @Field("global_device_id") String deviceId,  
    @Field("type") String type
);
Sign up to request clarification or add additional context in comments.

10 Comments

It does works but still I am unable to go inside the call.enqueue(new retrofit2.Callback<UIPResponse>()
It's because your json response model UIPResponse is wrong. Use jsonschema2pojo.org to create your model.
Yes I have created it with jsonpojo.
I have update my question please see my UIPResponse
Now your response model is correct. What error are you having?
|
0

If you havent, please add Internet permission in your manifest. You can look it up https://developer.android.com/training/basics/network-ops/connecting

Have you tried using other api? if not, please try it first. Just find some Dummy API in google. this is an example for you https://reqres.in/

I remember that okttp asks for secure api. i can see that yours is not using it. Please refer to this link https://developer.android.com/training/articles/security-config to trust all connection. You need to add it in your manifest but please read it first. Good luck

Comments

0

Did you initialize the library like it's described here? https://amitshekhar.me/Fast-Android-Networking/adding_fan_to_your_project.html

Also you can enable logging and check in logcat if it's executing the call:

https://amitshekhar.me/Fast-Android-Networking/logging.html

1 Comment

Yeah I see, and when you debug you are getting onResponse or onFailure event? Maybe use logcat to print throwable or response
-2

Accessing REST API is given in the Android developers guide. Try the example given here: https://developers.google.com/android/guides/http-auth

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.