如何在android中使用Retrofit for Parse JSON值?

时间:2017-09-27 08:02:01

标签: android json magento

您好我试图通过Retrofit方法使用Rest API从Magento获取数据但是我推荐了许多网站,但我无法获得任何希望,请帮助我编写正确的编码,我将会有所帮助..

我的JSON格式:

    @SerializedName("sku")
    @Expose
    private String sku;

    @SerializedName("name")
    @Expose
    private String name;

    @SerializedName("price")
    @Expose
    private String price;

    @SerializedName("extension_attributes")
    @Expose
    private ExtensionAttributes extensionAttributes;

    public String getSku() {
        return sku;
    }

    public void setSku(String sku) {
        this.sku = sku;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    public ExtensionAttributes getExtensionAttributes() {
        return extensionAttributes;
    }

    public void setExtensionAttributes(ExtensionAttributes extensionAttributes) {
        this.extensionAttributes = extensionAttributes;
    }

    @Override
    public String toString() {
        return "Product{" +
                "sku='" + sku + '\'' +
                ", name='" + name + '\'' +
                ", price='" + price + '\'' +
                ", extensionAttributes=" + extensionAttributes +
                '}';
    }
}

我需要的属性是: name,sku,price,extension_attributes:" media_gallery":" file",attribute_code:" special_price",value:" 121.6000"

我根据我的知识设置了一些编码如下:

产品:

公共类产品{

public class ExtensionAttributes {

    @SerializedName("media_gallery_entries")
    @Expose
    private ArrayList<Media_Gallery> media;

    public ArrayList<Media_Gallery> getMedia() {
        return media;
    }

    public void setMedia(ArrayList<Media_Gallery> media) {
        this.media = media;
    }
}

扩展属性:

    @SerializedName("file")
    @Expose
    private String file;

    public String getFile() {
        return file;
    }

    public void setFile(String file) {
        this.file = file;
    }
}

Media_Gallery:

公共类Media_Gallery {

public class MainActivity extends AppCompatActivity {

    RecyclerView recyclerView;
    List<Product> listing;
    List<Media_Gallery> listing1;

    private  final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recycle_retrofit);
        recyclerView = (RecyclerView) findViewById(R.id.recycle);
        listing = new ArrayList<>();
        listing1 = new ArrayList<Media_Gallery>();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://alagendransupermart.com/mageapi/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        APiService service = retrofit.create(APiService.class);
        Call<List<Product>> call=   service.getbookdetails();
        call.enqueue(new Callback<List<Product>>() {
            @Override
            public void onResponse(Call<List<Product>> call, Response<List<Product>> response) {

                List<Product> list = response.body();
                Product product = null;
                String image=null;
                for (int i =0 ;i<list.size();i++){
                    product = new Product();
                    String name = list.get(i).getName();
                    String color = list.get(i).getSku();
                    String price = list.get(i).getPrice();

                    ArrayList<Media_Gallery> media = response.body().get(i).getExtensionAttributes().getMedia();

                    for(int j = 0; j<media.size(); j++){
                         image = media.get(j).getFile();

                    }

                    Media_Gallery med = new Media_Gallery();
                    med.setFile(image);

                    product.setPrice(price);
                    product.setSku(color);
                    product.setName(name);
                    listing.add(product);
                    listing1.add(med);
                }


                Recycleradapter recyclerAdapter = new Recycleradapter(MainActivity.this,listing);
                RecyclerView.LayoutManager recyce = new GridLayoutManager(MainActivity.this,2);
                // RecyclerView.LayoutManager recyce = new LinearLayoutManager(MainActivity.this);
                recyclerView.addItemDecoration(new GridSpacingdecoration(2, dpToPx(10), true));
                recyclerView.setLayoutManager(recyce);
                recyclerView.setItemAnimator( new DefaultItemAnimator());
                recyclerView.setAdapter(recyclerAdapter);


            }

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

                Log.d(TAG,"Something went wrong :"+t.getMessage());


            }
        });

    }
}

请检查以上编码是否正确.. 但我怀疑在mainactivity中我应该如何在我的回收器视图中设置所有上述值,请帮助我当前的mainactivity编码如下:

class DrawRoundRectangle extends View {
Paint paint;
int strokWidth = 6;
Path path;
// define the rectangle
int offsetX = 75;
int offsetY = 100;
int horizLineLength = 240;
int vertLineLength = 40;
int cornerRadius = 100;

// calc some lengths for use in percent complete
int cornerLength = (int) (2 * cornerRadius * Math.PI);
int totalLength = cornerLength * 4 + horizLineLength * 2 + vertLineLength * 2;
MainActivity ma;

// calc at what accumulated length each part of the rect starts
int startT = 0;
int startTR = horizLineLength;
int startR = startTR + cornerLength;
int startBR = (int) (startR + vertLineLength);
int startB = startBR + cornerLength;
int startBL = startB + horizLineLength;
int startL = startBL + cornerLength;
int startTL = startL + vertLineLength;

// percent complete
int percent = 100;
private int accumLength;
private double d1;
private double d2;
private double d3;
private double d4;
private double d5;
private double d6;
private double d7;
private double d8;
int x1;
int y1;
int x2;
int y2;
int x;
int y;
double start;
double end1;
double end2;
double end3;
double end4;
Canvas canvas;

public DrawRoundRectangle(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    paint = new Paint();
    paint.setStrokeWidth(strokWidth);
    paint.setColor(Color.rgb(203,156,76));
    path = new Path();
    Paint.Style style = paint.getStyle();
    paint.setStyle(style.STROKE);
}

float progress = 0;
float stepLength = 0.5f;

@Override
protected void onDraw(Canvas canvas) {
    //canvas.drawPath(segment, paint);
        this.canvas = canvas;

    drawPercentRect(progress);
    progress+=stepLength;
    if(progress >= 100){
        progress = 0;
        path.reset();
    }
}

// draw the radius rectangle
private void resetVars(){
    d1=0;
    d2=0;
    d3=0;
    d4=0;
    d5=0;
    d6=0;
    d7=0;
    d8=0;
    end1=0;
    end2=0;
    end3=0;
    end4=0;
}
private void drawPercentRect(float percent) {

    // percent expressed as a length-traveled-along-rect
    accumLength = (int) (percent / 100 * totalLength);

    // clear the canvas
    //ctx.clearRect(0, 0, canvas.width, canvas.height);

    // top line
    resetVars();
    d1 = accumLength - startT;
    d1 = Math.min(d1, horizLineLength);
    stepLength = 0.5f;
    if (d1 > 0) {
        x1 = offsetX + cornerRadius;
        y1 = offsetY;
        x2 = (int) (offsetX + cornerRadius + d1);
        y2 = offsetY;
        drawLine(x1, y1, x2, y2);
    }

    // top-right corner
    d2 = accumLength - startTR;
    d2 = Math.min(d2, cornerLength);
    if (d2 > 0) {
        x = offsetX + 50 + horizLineLength;
        y = offsetY;
        start =  270;
        end1 =  90*(d2/cornerLength);
        drawCorner(x, y, start, end1);
    }

    // right line
    d3 = accumLength - startR;
    d3 = Math.min(d3, vertLineLength);
    if (d3 > 0) {
        x1 = offsetX + cornerRadius + horizLineLength + cornerRadius/2;
        y1 = offsetY + cornerRadius/2;
        x2 = offsetX + cornerRadius + horizLineLength + cornerRadius/2;
        y2 = (int) (offsetY + cornerRadius/2 + d3);
        drawLine(x1, y1, x2, y2);
    }

    // bottom-right corner
    d4 = accumLength - startBR;
    d4 = Math.min(d4, cornerLength);
    if (d4 > 0) {
        x = offsetX  + horizLineLength+cornerRadius/2;
        y = offsetY  + vertLineLength;
        start = 0;
        //end = (int) ((d / cornerLength) * Math.PI / 2);
        end2 = 90*(d4/cornerLength);
        drawCorner(x, y, start, end2);
    }

    // bottom line
    d5 = accumLength - startB;
    d5 = Math.min(d5, horizLineLength);
    if (d5 > 0) {
        x1 = offsetX + cornerRadius + horizLineLength;
        y1 = offsetY + cornerRadius + vertLineLength;
        x2 = (int) (offsetX + cornerRadius + horizLineLength - d5);
        y2 = offsetY + cornerRadius + vertLineLength;
        drawLine(x1, y1, x2, y2);
    }

    // bottom-left corner
    d6 = accumLength - startBL;
    d6 = Math.min(d6, cornerLength);
    if (d6 > 0) {
        x = offsetX + cornerRadius/2;
        y = offsetY + vertLineLength;
        start = 90;
        end3 = 90*(d6/cornerLength);
        drawCorner(x, y, start, end3);
    }

    // left line
    d7 = accumLength - startL;
    d7 = Math.min(d7, vertLineLength);
    if (d7 > 0) {
        x1 = offsetX + cornerRadius/2;
        y1 = offsetY + cornerRadius/2 + vertLineLength;
        x2 = offsetX + cornerRadius/2;
        y2 = (int) (offsetY + cornerRadius/2 + vertLineLength - d7);
        drawLine(x1, y1, x2, y2);
    }

    // top-left corner
    d8 = accumLength - startTL;
    d8 = Math.min(d8, cornerLength);
    if (d8 > 0) {
        x = offsetX+cornerRadius/2;
        y = offsetY;
        start = 180;
        end4 = 90*(d8/cornerLength);
        drawCorner(x, y, start, end4);
    }

}

private void drawLine(int x1, int y1, int x2, int y2) {
    //ctx.beginPath();
    path.moveTo(x1, y1);
    path.lineTo(x2, y2);
    canvas.drawPath(path, paint);
    //ctx.stroke();
}

private void drawCorner(int x, int y, double start, double end) {
    if(end<90){
        progress+=4*stepLength;
    }
    path.arcTo(x, y, x + cornerRadius, y + cornerRadius, (float) start(float) end,true);
    canvas.drawPath(path, paint);
}

public void start(final MainActivity ma) {
    this.ma = ma;
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                ma.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        invalidate();
                    }
                });
                try {
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }).start();

}
}

0 个答案:

没有答案