使用Retrofit 2.0获取Webservice请求的结果

时间:2018-03-01 19:44:59

标签: android json retrofit2

我正在使用Retrofit而且我正在接触我的网络服务,但我无法得到答案和我想要的字段。示例:campo1并分配给变量并打印它。 我想我没有正确地调用响应,因为它是空的

发送JSON:

{
    "select":"select id || ' | ' ||senha_terminal,nome,login_web,null,5,null n2,7,senha_terminal,senha_web,data_inclusao from usuario where id in (3257) order by id desc"
}

预期回应:

[
    {
        "campo1": "3257 | 74327",
        "campo2": "Sidnei",
        "campo3": "sidnei01",
        "campo4": null,
        "campo5": 5,
        "campo6": null,
        "campo7": 7,
        "campo8": "74327",
        "campo9": "56c07af798f309dbd75822a849ce47b6",
        "campo10": "2012-02-08T11:00:06"
    }
]

我想获得field1并在变量响应中赋值。

主要:

package com.example.romeu.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

import org.json.JSONException;
import org.json.JSONObject;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class MainActivity extends AppCompatActivity implements Callback<SelectModel> {

    public Button btnConsultar;
    Call<SelectModel> call;
    Response<SelectModel> response;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ApiInterface.URL_BASE)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        ApiInterface apiInterface = retrofit.create(ApiInterface.class);
        try
        {
            JSONObject paramObject = new JSONObject();
            paramObject.put("select", "select id || ' | ' ||senha_terminal,nome,login_web,null,5,null n2,7,senha_terminal,senha_web,data_inclusao from usuario where id in (3257) order by id desc");
            onResponse(call, response);
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void onResponse(Call<SelectModel> call, Response<SelectModel> response)
    {
        String resposta = response.body().getCampo1();
        Log.i("MSG", resposta);
    }

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

ApiInterface:

    package com.example.romeu.myapplication;

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.Headers;
import retrofit2.http.POST;

/**
 * Created by romeu on 01/03/18.
 */

public interface ApiInterface {
    String URL_BASE = "https://services-dev.redetendencia.com.br/api-rest";

    @Headers("Content-Type: application/json")
    @POST("select")
    Call<SelectModel> getSelect(@Body String body);

}

型号:

package com.example.romeu.myapplication;

/**
 * Created by romeu on 01/03/18.
 */

class SelectModel {
    public String campo1,campo2,campo3,campo4,campo5,campo6,campo7,campo8,campo9,campo10;

    public SelectModel(){}

    //Getters


    public String getCampo1() {
        return campo1;
    }

    public String getCampo2() {
        return campo2;
    }

    public String getCampo3() {
        return campo3;
    }

    public String getCampo4() {
        return campo4;
    }

    public String getCampo5() {
        return campo5;
    }

    public String getCampo6() {
        return campo6;
    }

    public String getCampo7() {
        return campo7;
    }

    public String getCampo8() {
        return campo8;
    }

    public String getCampo9() {
        return campo9;
    }

    public String getCampo10() {
        return campo10;
    }
}

1 个答案:

答案 0 :(得分:0)

我认为您正在尝试实现异步请求。但回调不会这样:

onResponse(call, response);

您实际上只是直接使用onResponse( .. )响应来调用null。此外,您根本不构建Callnull。将以上行替换为:

Call<SelectModel> call = apiInterface.getSelect(paramObject.toString());
call.enqueue(MainActivity.this);

注意paramObject.toString()只是一个建议。我不确定应该如何附加到请求正文。也许你只能在没有任何JsonObject的情况下放置该字符串。

将异步请求排入队列,当获得响应时,将调用onResponse( .. )