ilvthsgm
ilvthsgm

Reputation: 636

Retrofit Send String -No Json

I want to send string as a get request via Retrofit and get response as a string as well. To being more clear, I don't want to use Json.

Send data example:

http://192.168.1.2:51276/deneme.aspx?1231easc*eadd;5;

Response data example:

dsa<#12no password;+;#yes;

Interface:

@GET("/deneme.aspx")
    Call<ServerResponse> get(
            @Query("") String method
    );

I added ScalarsConverterFactory but doesn't let me use only string.

public void loginGet(String strg_no){

        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.addInterceptor(logging);
        Retrofit retrofit = new Retrofit.Builder()
                .client(httpClient.build())
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(SERVER_URL)
                .build();

        Log.d("SIGNAL", "strg_no: " + strg_no);
        Interface service = retrofit.create(Interface.class);

        Call<ServerResponse> call = service.get(strg_no);

        call.enqueue(new Callback<ServerResponse>() {
            @Override
            public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
                // response.isSuccessful() is true if the response code is 2xx
                BusProvider.getInstance().post(new ServerEvent(response.body()));
                Log.d("SIGNAL", "response body : " + response.body());
                Log.e(TAG,"Success");
            }

            @Override
            public void onFailure(Call<ServerResponse> call, Throwable t) {
                // handle execution failures like no internet connectivity
                BusProvider.getInstance().post(new ErrorEvent(-2,t.getMessage()));
            }
        });
    }

Server response:

public class ServerResponse {
    @SerializedName("returned_ strg_no")
    private String strg_no;
    @SerializedName("error_code")
    private int errorCode;
    private int status = 1;
    private String error;

    public ServerResponse(String strg_no, int errorCode, int status, String error){
        this. strg_no = strg_no;
        this.errorCode = errorCode;
        this.status = status;
        this.error = error;
    }

    public String getstrg_no() {
        return strg_no;
    }
}

Server Event:

public class ServerEvent {
    private ServerResponse serverResponse;

    public ServerEvent(ServerResponse serverResponse) {
        this.serverResponse = serverResponse;
    }

    public ServerResponse getServerResponse() {
        return serverResponse;
    }

    public void setServerResponse(ServerResponse serverResponse) {
        this.serverResponse = serverResponse;
    }
}

How can I send and get data without using Json?

Upvotes: 0

Views: 213

Answers (2)

Hardik Vasani
Hardik Vasani

Reputation: 876

i hpoe it will help you!

@GET("/deneme.aspx")
Call<ResponseBody> get(
        @Query("") String method
);


Call<ResponseBody> call = service.get(strg_no);

  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
  }});

Upvotes: 1

Navneet Krishna
Navneet Krishna

Reputation: 5017

Try @Path instead of @Query

@GET("/deneme.aspx{query}")
Call<ServerResponse> get(@Path("query") String method);

and call like this

Call<ServerResponse> call = service.get("?"+strg_no);

Or

try with dynamic url

@GET
Call<ServerResponse> get(@Url String url);

and call like this

Call<ServerResponse> call = service.get("http://192.168.1.2:51276/deneme.aspx?"+strg_no);

Upvotes: 1

Related Questions