How to POST list of objects as JSON from Android client to PHP REST API?

…衆ロ難τιáo~ 提交于 2019-12-12 04:33:29

问题


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.


回答1:


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);



回答2:


Use this code to send Json data

 try{

            JSONObject jsonObject = new JSONObject();
            jsonObject.accumulate("projectid","1");
            jsonObject.accumulate("cnumber", "8983899383");
            jsonObject.accumulate("address","IN");
            jsonObject.accumulate("companyid","5");
            jsonObject.accumulate("uploadimage","");
            jsonObject.accumulate("id","9")

            data = jsonObject.toString();
            Log.d("json data",data);
            // 1. create HttpClient
            HttpClient httpclient = new DefaultHttpClient();

            // 2. make POST request to the given URL
            HttpPost httpPost = new HttpPost(CHECK_WEBSERVICE_URL);
            StringEntity se = new StringEntity(data);

            // 6. set httpPost Entity
            httpPost.setEntity(se);

            // 7. Set some headers to inform server about the type of the content
            httpPost.setHeader("Accept", "application/json");
            httpPost.setHeader("Content-type", "application/json");

            // 8. Execute POST request to the given URL
            HttpResponse httpResponse = httpclient.execute(httpPost);

            // 9. receive response as inputStream
            inputStream = httpResponse.getEntity().getContent();


来源:https://stackoverflow.com/questions/40542834/how-to-post-list-of-objects-as-json-from-android-client-to-php-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!