如何将对象列表作为JSON从Android客户端发布到PHP REST API? [英] How to POST list of objects as JSON from Android client to PHP REST API?

查看:201
本文介绍了如何将对象列表作为JSON从Android客户端发布到PHP REST API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:



REST API:PHP Slim Framework
Android HTTP客户端库:loopj



我将一个ITEM存储为sqlite中的json字符串。



我想在服务器上发布这个JSON。每个项目都是我的SQLite数据库中的记录。



我目前在做什么?
我有JSON的对象列表。我想将它发布到我的基于PHP的REST API。



这是代码

  if(cursor!= null&& cursor.getCount()> 0){
if(cursor.moveToFirst()){
do {
Item item = new Item();

try {

item.setOfflineId(Long.parseLong(cursor.getString(0)));
item.setName(cursor.getString(1));


item.setImageLocalPath(itemPayload.getImageLocalPath());


item.setToUpdate(Boolean.parseBoolean(cursor.getString(3).toString()));
item.setDeviceID(cursor.getString(4));
item.setCreatedDate(cursor.getString(5));
//item.setImageBlob(cursor.getBlob(5));

String imageURL = itemPayload.getImageLocalPath();
文件imageFile = new File(imageURL);


} catch(例外e){
Log.d(Ex,e.getMessage()。toString());
}


itemList.add(item);
} while(cursor.moveToNext());
}
}
} catch(SQLException ex){
Log.d(DB,ex.getMessage()。toString());
}

挑战?



<当我把params用于发布时,我还将一个Param设置为File,因为我也想上传一个图像文件。我知道如何将一个对象作为JSON发送。我想POST项目列表(具有图像文件的项目)。怎么做到这一点?



一种方法是为列表中的每个对象执行API POST。不确定这是否是一个好方法。



寻找建议。

解决方案

你可以用这个发布json方式

  JSONObject json = new JSONObject(); 
json.put(obj,obj value);
StringEntity entity = new StringEntity(json.toString());
client.post(context,url,entity,application / json,
responseHandler);


Scenario:

REST API: PHP Slim Framework Android HTTP client library: loopj

I am storing an ITEM as json string in sqlite.

I want to POST this JSON on server. Each item is a record in my SQLite database.

What I am currently doing? I have JSON with list of objects. I want to POST it to my PHP based REST API.

Here is the code

if (cursor != null && cursor.getCount() > 0) {
            if (cursor.moveToFirst()) {
                do {
                    Item item = new Item();

                    try {

                        item.setOfflineId(Long.parseLong(cursor.getString(0)));
                        item.setName(cursor.getString(1));


                        item.setImageLocalPath(itemPayload.getImageLocalPath());


                        item.setToUpdate(Boolean.parseBoolean(cursor.getString(3).toString()));
                        item.setDeviceID(cursor.getString(4));
                        item.setCreatedDate(cursor.getString(5));
                        //item.setImageBlob(cursor.getBlob(5));

                        String imageURL = itemPayload.getImageLocalPath();
                        File imageFile = new File(imageURL);


                    } catch (Exception e) {
                        Log.d("Ex", e.getMessage().toString());
                    }


                    itemList.add(item);
                } while (cursor.moveToNext());
            }
        }
    }catch(SQLException ex){
        Log.d("DB", ex.getMessage().toString());
    }

The challenge?

When I put the params for post, I also set one Param to File as I want to upload an image file as well. I know how to send one object as JSON. I want to POST the list of items (items having image Files). How to achieve this?

One way is that I do an API POST for each object in the list. Not sure if it is a good way to do it.

Looking for advice.

解决方案

You can post json using this way

JSONObject json = new JSONObject();
json.put("obj", "obj value");
StringEntity entity = new StringEntity(json.toString());
client.post(context, url, entity, "application/json",
    responseHandler);

这篇关于如何将对象列表作为JSON从Android客户端发布到PHP REST API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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