Android HttpPost请求服务器

时间:2015-08-20 20:29:20

标签: android sqlite codeigniter http-post

我想将数据从Android SqLite数据库发送到我的Codeigniter Web App控制器,该控制器获取发布数据并将其保存到MySql数据库 所以我在android app和它的代码中做了这个同步任务:

    public void syncAttendance(View v) {
    try
    {
    /** Retrieving data from database **/
    //use cursor to keep all data
    //cursor can keep data of any data type
    Cursor c=db.rawQuery("select * from mytable", null);
    int memNum = 1;
    //move cursor to first position
    c.moveToFirst();
    //fetch all data one by one
    do
    {
        //we can use c.getString(0) here
        //or we can get data using column index
        String memID = c.getString(c.getColumnIndex("memID"));
        String currTime = c.getString(c.getColumnIndex("currTime"));
        String dayName = c.getString(c.getColumnIndex("dayName"));

        ////////////////////////////////////////

     // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://mywebsite.com/index.php/admin/attendance/scan_qr");
        JSONObject json = new JSONObject();

      /**  try {**/
            // JSON data:
            json.put("memID", memID);
            json.put("currTime", currTime);
            json.put("dayName", dayName);

            JSONArray postjson=new JSONArray();
            postjson.put(json);

            // Post the data:
            httppost.setHeader("json",json.toString());
            httppost.getParams().setParameter("jsonpost",postjson);

            // Execute HTTP Post Request
            System.out.print(json);
            HttpResponse response = httpclient.execute(httppost);

            // for JSON:
            if(response != null)
            {
                InputStream is = response.getEntity().getContent();

                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                StringBuilder sb = new StringBuilder();

                String line = null;
                try {
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                text = sb.toString();
            }

            //tv.setText(text);
 /**
        }catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
        **/
        //////////////////////////////////////////

        /** show confirmation toast **/

        Toast toast = Toast.makeText(this, memNum + memID + currTime + dayName, Toast.LENGTH_LONG);
        toast.show();
        memNum ++;
        //move next position until end of the data
    }while(c.moveToNext());

    /** show confirmation toast **/
    Toast toast = Toast.makeText(this, "Synchronization Completed", Toast.LENGTH_LONG);
    toast.show();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

}

和php codeigniter控制器代码是:

public function scan_qr()
{

        $json = $_SERVER['HTTP_JSON'];
        var_dump($json);

        $data = json_decode($json);
        var_dump($data);

        $memID = $data->memID;
        $currTime = $data->currTime;
        $dayName = $data->dayName;
        $ma7abawy_year = 3;

        $data= array(
                        'member_id' => $memID,
                        //'points' => $points,
                        'presence_time' => $currTime,
                        //'event' => $eventname,
                        'event_date' => $dayName,
                        'ma7abawy_year' => $ma7abawy_year
                    );
        $this->db->insert('attendance',$data);

}

我也不例外但没有发生任何事情

任何想法都将不胜感激

1 个答案:

答案 0 :(得分:0)

我建议使用改装(请参阅API docs)库来使用该服务。然后,看看你在响应中得到了什么,将数据保存到sqlite3 db将是一件简单的事情。有关保存数据,请参阅example