2015-08-20 73 views
0

我想從Android SqLite數據庫發送數據到我的Codeigniter網絡應用程序控制器,它將發佈數據並將其保存到MySql數據庫 所以我做了buttn在android應用程序和它的代碼中進行此同步任務:Android HttpPost請求到服務器

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笨控制器代碼:

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

} 

我有沒有例外,但任何反應

任何想法將不勝感激

+0

你將數據傳遞給標題的任何原因?爲什麼不是傳統的帖子名稱=值密鑰對? –

+0

怎麼能從android?這是問題嗎? –

回答

0

我會建議使用改造(見API docs)庫消耗的服務。之後,看看你在迴應中得到了什麼,並且將數據保存到sqlite3數據庫中是一件簡單的事情。請參閱example以保存數據。

+0

感謝您的回放,我是一個非常新手在Java和Android,所以我不明白你的意思,我想讓你知道我的應用程序是爲掃描學院成員QR代碼出席和存儲出席SQLite數據庫在互聯網連接可用時同步這些數據,我成功掃描,存儲和檢索數據,但我無法通過我的codeigniter web應用程序將這些出勤數據傳輸到MySQL數據庫 –