如何从android studio接收Php中的Json数组并将其保存在mysql中

时间:2015-08-08 20:41:17

标签: php android json http

我想收到JSON数组并将其保存到SQLite数据库中?我采取的步骤如下:

  • 我在Android Studio中创建了一个SQLite数据库。
  • 单击按钮时,我将SQLite数据库转换为JSON数组
  • 通过Http请求发送数组

共有四列。

try {

                // 1. create HttpClient
                HttpClient httpclient = new DefaultHttpClient();

                // 2. make POST request to the given URL
                HttpPost httpPost = new HttpPost( "http://192.168.1.7/sql.php");

//TotalRecord=jsonarray.toString();

                StringEntity se = new StringEntity(TotalRecord);
                abc=true;

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

使用这些列我想将它们内部的数据接收到JSON数组中,在PHP中实现这一点的任何方法?

邮政结构是:

            Cursor res = myDB.getAllData();
            if (res.getCount() == 0) {
                showMessage("error", "nothing to disply");
                return;
            }
            JSONArray jsonArray = new JSONArray();
            //JSONObject jsonObject=new JSONObject();
            StringBuffer stringBuffer = new StringBuffer();
      while (res.moveToNext()) {// Converted whole sqlite database in Json
                JSONObject jsonObject = new JSONObject();
                String id = "";
                String name = "";
                String surname = "";
                String marks = "";
                id = String.valueOf(stringBuffer.append(res.getString(0)));
                name = String.valueOf(stringBuffer.append(res.getString(1)));
                surname = String.valueOf(stringBuffer.append(res.getString(0)));
                marks = String.valueOf(stringBuffer.append(res.getString(0)));
                try {
                    jsonObject.accumulate("ID", id);
                    jsonObject.accumulate("Name", name);
                    jsonObject.accumulate("SurName", surname);
                    jsonObject.accumulate("Marks", marks);
                    jsonArray.put(jsonObject);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                TotalRecord = jsonArray.toString();

0 个答案:

没有答案