需要PHP代码将来自android的JSONArray存储到远程数据库中

时间:2014-11-10 02:40:53

标签: php android json android-json

我的目标是将本地数据库的内容添加到远程数据库中。 我将数据库的内容作为自定义对象添加到JSONArray中。 我将此JSONArray转换为字符串并将此字符串作为POST参数传递到php页面,但我无法编写php代码将此内容存储在服务器端的数据库中。 请帮我写PHP代码

从数据库获取数据并存储到JSONArray

DataEntry d=new DataEntry(context);
JSONArray call_records=d.get();
String update_content=call_records.toString();
success= new GetUpdateStatus().execute(update_content).get();

DataEntry类的get()方法

public JSONArray get() throws JSONException
    {   
        JSONArray CallList = new JSONArray();

        String selectQuery = "SELECT  * FROM " + DATABASE_TABLE +";";
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
        do {
                CallRecord record = new CallRecord();
                record.setId(cursor.getInt(0));
                record.setCaller(cursor.getString(1));
                record.setReciever(cursor.getString(2));
                record.setMapUrl(cursor.getString(3));
                record.setTimeStamp(String.valueOf((cursor.getType(4))));


                CallList.put(record.getJSONObject());

            } while (cursor.moveToNext());
        }
        return CallList;
    }

CallRecord类:

public class CallRecord {
    //private variables
    int _id;
    String _caller;
    String _reciever;
    String _map;
    String _ts;
//This class has set and get methods for each variable.
}

JSON解析器代码:

protected Integer doInBackground(String... a) {
        int success=0;
        try {         
            List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("add",a[0]));
                JSONObject json = jsonParser.makeHttpRequest(
                               url_update, "POST", params);

和此:

if(method == "POST"){
            // request method is POST
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

2 个答案:

答案 0 :(得分:0)

您是否尝试获取原始POST数据。

$post = file_get_contents('php://input');

答案 1 :(得分:0)

使用此方法,您将获得所有发布数据作为$ data变量中的数组。

$ data = json_decode(file_get_contents('php:// input'),true);