关于okhttputils的简单使用(含 XRecyclerView的加载,刷新)2

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.example.lenovo.shishnagyuan.MainActivity;
import com.example.lenovo.shishnagyuan.R;
import com.example.lenovo.shishnagyuan.adapter.MyClosetSQList;
import com.example.lenovo.shishnagyuan.adapter.MySp;
import com.example.lenovo.shishnagyuan.adapter.Viewpager;
import com.example.lenovo.shishnagyuan.url.MyCommodityJavaBean;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.Serializable;
import java.util.ArrayList;

public class ShangPinX extends AppCompatActivity implements View.OnClickListener{
private Button xq_gm;
private RelativeLayout xq_gwc;
private TextView xq_log1,xq_log2,xq_jg,xq_ckj,xq_wodeyichu,xq_text,xq_jgg;
private ImageView xq_heixin,xq_wodeyic;
private String name;
private int id1;
private ViewPager xq_img1;
private String sqlid;
private long i;
private android.support.v4.app.FragmentManager fm;
private ArrayList<Fragment> liat;
private Viewpager adapter;
private String thumb,price;
private Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int arg1 = msg.arg1;
if (i%2==0){
xq_img1.setCurrentItem(0);
}else {
xq_img1.setCurrentItem(1);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shang_pin_x);

xq_gm = (Button) findViewById(R.id.xq_gm);
xq_log1 = (TextView) findViewById(R.id.xq_log1);
xq_log2 = (TextView) findViewById(R.id.xq_log2);
xq_jg = (TextView) findViewById(R.id.xq_jg);
xq_ckj = (TextView) findViewById(R.id.xq_ckj);
xq_img1 = (ViewPager) findViewById(R.id.xq_img1);
xq_heixin = (ImageView) findViewById(R.id.xq_heixin);
xq_wodeyic = (ImageView) findViewById(R.id.xq_wodeyic);
xq_wodeyichu = (TextView) findViewById(R.id.xq_wodeyichu);
xq_gwc = (RelativeLayout) findViewById(R.id.xq_gwc);
xq_text = (TextView) findViewById(R.id.xq_text);
xq_jgg= (TextView) findViewById(R.id.xq_jgg);

Intent intent=getIntent();
Serializable id = intent.getSerializableExtra("id1");
String str=(String) id;
id1=Integer.parseInt(str);
String url="http://atp.fulishe.com/ClientApi/category.php?api_version=1.0&act=search_category_goods_list&" +
"c_id=35&order_price=0&page_num=20&page=1&debug=true&client_id=null";
OkHttpUtils
.get()
.url(url)
.build()
.execute(new StringCallback() {
@Override
public void onError(Request request, Exception e) {
}
@Override
public void onResponse(String response) {
MyCommodityJavaBean javaBrea= JSONObject.parseObject(response,MyCommodityJavaBean.class);
price=javaBrea.getInfo().getGoods().get(id1).getPromote_price();
name=javaBrea.getInfo().getGoods().get(id1).getGoods_name();
xq_log1.setText(name);
thumb = javaBrea.getInfo().getGoods().get(id1).getThumb();
xq_log2.setText(javaBrea.getInfo().getGoods().get(id1).getEnglish_name());
// xq_ckj.append(javaBrea.getInfo().getGoods().get(id1).getGoods_id());
xq_text.setText(javaBrea.getInfo().getGoods().get(id1).getDescription());
xq_jg.setText(javaBrea.getInfo().getGoods().get(id1).getCurrency_price()+"");
xq_jgg.setText(javaBrea.getInfo().getGoods().get(id1).getCurrency_price()+"");
fm=getSupportFragmentManager();
liat=new ArrayList<>();
liat.add(new Ncarousel(thumb));
liat.add(new NcarouselA());
xq_img1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return true;
}
});
adapter = new Viewpager(fm,liat);
xq_img1.setAdapter(adapter);
new Thread(){
@Override
public void run() {
super.run();
while (true){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Message message=new Message();
message.arg1=(int)i++;
handler.sendMessage(message);
}
}
}.start();
}
});
xq_gwc.setOnClickListener(this);
}
















@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.xq_gwc:
MySp sp = new MySp();
String flag = sp.getFlag(ShangPinX.this);
if (flag.equals("成功")){
String gwc = sp.getGWC(ShangPinX.this);
if (gwc.equals("0")){
xq_wodeyichu.setTextColor(Color.parseColor("#FFB6C1"));
xq_heixin.setImageResource(R.drawable.xiaoheixin);
MyClosetSQList sqList=new MyClosetSQList(ShangPinX.this);
ContentValues values=new ContentValues();
values.put("img",thumb);
values.put("name",name);
values.put("price",price);
sqList.insert(values);
sp.setGWC(ShangPinX.this,"1");
Toast.makeText(this, "你已经加入到购物车", Toast.LENGTH_SHORT).show();
}else {
xq_wodeyichu.setTextColor(Color.parseColor("#000000"));
xq_heixin.setImageResource(R.drawable.heixin);
MyClosetSQList sqList=new MyClosetSQList(ShangPinX.this);
sp.setGWC(ShangPinX.this,"0");
Cursor cursor=sqList.query();
while (cursor.moveToNext()){
}
}
}else {
Intent intent=new Intent(ShangPinX.this, MainActivity.class);
startActivity(intent);
}
break;
}
}
}






//布局

<ImageView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="15dp"
android:id="@+id/newon_iv"/>

<LinearLayout
android:background="@mipmap/mengceng"
android:layout_height="50dp"
android:layout_width="270dp"
android:orientation="vertical"
android:layout_marginLeft="15dp"
android:layout_alignLeft="@+id/newon_iv"
android:layout_alignRight="@+id/newon_iv"
android:layout_alignBottom="@+id/newon_iv">

<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/newon_tv1"
android:layout_marginLeft="10dp"
android:textColor="#ffffff"
android:textSize="18dp"/>

<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal">

<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/newon_tv2"
android:layout_marginLeft="10dp"
android:textColor="#ffffff"
android:textSize="18dp"
android:singleLine="true"
android:ems="4"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/newon_tv3"
android:layout_marginLeft="40dp"
android:textColor="#ffffff"
android:textSize="18dp"
android:singleLine="true"
android:ems="6"
android:paddingLeft="20dp"/>
</LinearLayout>
</LinearLayout>





原文地址:https://www.cnblogs.com/yudada/p/7571287.html