如何通过 Retrofit to text view 检索 Web 服务值? [英] How to retrieve web service values through Retrofit to text view?

查看:61
本文介绍了如何通过 Retrofit to text view 检索 Web 服务值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,在第一个活动中它具有卡片视图布局.我正在从网络服务中检索数据,相关数据显示在卡片视图中.它运作良好.现在,当用户单击特定卡片视图时,我需要进行另一项活动.我正在获取该卡片视图的相关 ID 并将其传递给第二个活动.在第二个活动中,我需要根据该唯一 ID 显示内容.但我没有得到任何东西.这是我尝试过的.

I'm developing an app and in first activity it has card view layout. I'm retrieving data from a webservice and relevant data are showed in card view. It's working well. Now when a user clicks a particular card view I need to go for another activity. I'm getting relevant ID for that card view and passing it to the second activity. In second activity I need to show the content according to that unique Id. But I'm not getting any thing. This is what I tried.

Pojo 类

public class PromoDetails {

String PromoId;
String PromoName;
String PromoImg;
String promoDetails;
String promoValidty;

public PromoDetails(String PromoId, String PromoName, String PromoImg , String promoDetails , String promoValidity) {
    this.PromoId = PromoId;
    this.PromoName = PromoName;
    this.PromoImg = PromoImg;
    this.promoDetails = promoDetails;
    this.promoValidty = promoValidity;
}



public String getPromoId() {
    return PromoId;
}

public void setPromoId(String promoId) {
    PromoId = promoId;
}

public String getPromoName() {
    return PromoName;
}

public void setPromoName(String promoName) {
    PromoName = promoName;
}

public String getPromoImg() {
    return PromoImg;
}

public void setPromoImg(String promoImg) {
    PromoImg = promoImg;
}

public String getPromoDetails() {
    return promoDetails;
}

public void setPromoDetails(String promoDetails) {
    this.promoDetails = promoDetails;
}

public String getPromoValidty() {
    return promoValidty;
}

public void setPromoValidty(String promoValidty) {
    this.promoValidty = promoValidty;
}}

API接口

public interface ApiInterface {


@POST("ap/promotions.php")
Call<List<Promotions>> getPromotions();

@GET("test.php/promotions/{PromoId}")
Call<List<PromoDetails>> getPromotDetails(@Path("PromoId") String PromoId) ;}

新的活动类

public class PromotionsInside extends Activity {


private ApiInterface apiInterface;
private List<PromoDetails> promoDetails;
TextView prDescription;



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


    Bundle extras = getIntent().getExtras();
     String promoId = "";

    if (extras != null) {
        promoId = extras.getString("PROMO_ID");


      getPromotionUpdate(promoId);
    }

}


private void getPromotionUpdate(String myPromoId) {


    apiInterface = ApiClient.getApiClient().create(ApiInterface.class);


    Call<List<PromoDetails>> call = apiInterface.getPromotDetails(myPromoId);
    call.enqueue(new Callback<List<PromoDetails>>() {
        @Override
        public void onResponse(Call<List<PromoDetails>> call, Response<List<PromoDetails>> response) {
            promoDetails = response.body();

            runOnUiThread(new Runnable() {
                @Override
                public void run() {

 prDescription = (TextView)findViewById(R.id.promoDescriptionsss) ;
 prDescription.setText(promoDetails.get(0).getPromoName());
                }
            });


        }

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

        }
    });


}}

推荐答案

我也有类似的情况.尝试使用它来启动第二个活动:

I have similar case. Try to use this to start second activity:

    Intent intent = new Intent(this, PromotionsInside.class);
//Make sure that you put String id in intent
    intent.putExtra("PROMO_ID", id);

    startActivity(intent);

这在第二个活动中:

if (getIntent().hasExtra("PROMO_ID")) {
        String id = getIntent().getStringExtra("PROMO_ID", null);
        //next steps that you need
    }

希望能帮到你

这篇关于如何通过 Retrofit to text view 检索 Web 服务值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆