dqfaom241360 2015-08-08 20:41
浏览 90

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

I would like to like to receive the JSON array and save it to an SQLite database? The steps I have taken are as follows:

  • I have created an SQLite database in Android Studio.
  • I converted the SQLite database into a JSON array, when a button is clicked
  • Through a Http request the array is sent

There are four columns.

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

With those columns I would like to recieve the data inside them into the JSON array, Any way of achieving this in PHP?

Post Structure is :

            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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog