2013-08-23 23 views
2

我寫一個原生Android應用程序使用ID服務器數據,其中我使用PHP MYSQL從服務器獲取如何從Android中

數據在這[預約列表]我允許用戶重新安排預約,但每當我點擊獲取空白表單的項目時,總之沒有獲得我點擊列表中的特定約會的數據。

問題

如何顯示在使用AppointmentID表單數據?

下面我顯示我寫的所有必需的代碼[客戶&服務器端都]

UpcomingActivity.java:

 @Override 
     public boolean onContextItemSelected(MenuItem item) { 
      AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo(); 
      int menuItemIndex = item.getItemId(); 
      String[] menuItems = Cmd; 
      String CmdName = menuItems[menuItemIndex]; 
      // Check Event Command 
      if ("Cancel".equals(CmdName)) 
      { 
       Toast.makeText(UpcomingActivity.this,"Selected Cancel",Toast.LENGTH_LONG).show();   
      } 
      else if ("Reschedule".equals(CmdName)) 
      { 
       Toast.makeText(UpcomingActivity.this,"Selected Update",Toast.LENGTH_LONG).show(); 

       String sAppointmentID = MyArrList.get(info.position).get("UserID").toString();      
       Log.d(tag, "sAppointmentID :: " + sAppointmentID); 

       Intent newActivity = new Intent(UpcomingActivity.this, UpdateActivity.class); 
       newActivity.putExtra("UserID", sAppointmentID);      
       startActivity(newActivity); 
      }    
      return true; 
     } 

UpdateActivity.java:

  public void showInfo() 
      { 
       final TextView tAppointmentID = (TextView)findViewById(R.id.txtUsername); 
       final TextView tType = (TextView)findViewById(R.id.txtName); 
       final TextView tDate = (TextView)findViewById(R.id.txtEmail); 
       final TextView tTime = (TextView)findViewById(R.id.txtTel); 

       Button btnSave = (Button) findViewById(R.id.btnSave); 
       Button btnCancel = (Button) findViewById(R.id.btnCancel); 
       String url = "http://10.0.2.2/appointments/getByMemberID.php"; 

       Intent intent= getIntent(); 
       final String AppointmentID = intent.getStringExtra("AppointmentID"); 
       List<NameValuePair> params = new ArrayList<NameValuePair>(); 
       params.add(new BasicNameValuePair("sAppointmentID", AppointmentID)); 

       String resultServer = getHttpPost(url,params); 
       String strAppointmentID = "";    
       String strType = ""; 
       String strDate = ""; 
       String strTime = ""; 

       JSONObject c; 
       try { 
        c = new JSONObject(resultServer); 

        strAppointmentID = c.getString("UserID"); 
        Log.d(TAG, "String strAppointmentID" + strAppointmentID); 
        strType = c.getString("Type"); 
        Log.d(TAG, "String strType" + strType); 
        strDate = c.getString("Date"); 
        Log.d(TAG, "String strDate" + strDate); 
        strTime = c.getString("Time"); 
        Log.d(TAG, "String strTime" + strTime); 

        if(!strAppointmentID.equals("")) 
        { 
         tAppointmentID.setText(strAppointmentID); 
         tType.setText(strType); 
         tDate.setText(strDate); 
         tTime.setText(strTime); 
        } 
        else 
        { 
         tAppointmentID.setText("-"); 
         tType.setText("-"); 
         tDate.setText("-"); 
         tTime.setText("-"); 
         btnSave.setEnabled(false); 
         btnCancel.requestFocus(); 
        } 

       } catch (JSONException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
      } 
+0

首先確認錯誤該PHP是好的。爲此,請在** getByMemberID.php **中手動輸入ID並從Web瀏覽器加載它。 10.0.2.2/appointments/getByMemberID.php – Nizam

+0

請注意SQL注入攻擊。您目前的代碼是開放的,因爲您直接輸入用戶輸入($ _POST)到您的查詢中。谷歌它 - 如果你使用PHP的'mysqli'包(或者'mysql'的其他選擇),那麼它很快修復,或者在使用它之前對輸入執行一些檢查。 –

回答

2
  1. 首先是確保您擁有有效AppointmentIDUserId

  2. UpcomingActivity.java的方法onContextItemSelected您沒有提供AppointmentID代替你只提供UserIDIntend

  3. 列表但Updateactivity在方法showData您正在請求intent.getStringExtra("AppointmentID")這是invaild。

讓你更新你的UpcomingActivity.java應該是這樣的

public boolean onContextItemSelected(MenuItem item) { 
     AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo(); 
     int menuItemIndex = item.getItemId(); 
     String[] menuItems = Cmd; 
     String CmdName = menuItems[menuItemIndex]; 
     // Check Event Command 
     if ("Cancel".equals(CmdName)) 
     { 
      Toast.makeText(UpcomingActivity.this,"Selected Cancel",Toast.LENGTH_LONG).show();   
     } 
     else if ("Reschedule".equals(CmdName)) 
     { 
      Toast.makeText(UpcomingActivity.this,"Selected Update",Toast.LENGTH_LONG).show(); 

      String sAppointmentID = MyArrList.get(info.position).get("UserID").toString();      
      Log.d(tag, "sAppointmentID :: " + sAppointmentID); 

      Intent newActivity = new Intent(UpcomingActivity.this, UpdateActivity.class); 
      newActivity.putExtra("UserID", sAppointmentID); 
      newActivity.putExtra("AppointmentID", MyArrList.get(info.position).get("AppointmentID").toString());// <== here      
      startActivity(newActivity); 
     }    
     return true; 
    } 
+0

謝謝,但現在我無法更新數據,請檢查在這裏:http://stackoverflow.com/questions/18606478/how-to-update-data-using-php-mysql-in-android – Sneha

3
  • 選項1

    開發Java EE web服務並將其託管在服務器上。您的Android應用程序需要將數據與Web服務關聯並將其發送到服務器。服務器可以接收數據並按照它的要求進行操作。服務器還可以通過web服務的相同響應來處理和響應更多信息。您需要爲hello world Java EE提供Google。我找到了一個。

    一旦您的Java EE在本地桌面(或開發機器)上工作,您可以將WAR或EAR複製到EC2,以使應用程序可以通過互聯網工作,而無需經歷創建網絡的痛苦這是開放的和DMZ。

  • 選項2

    你可以開發一個基於java的插座產品,你的Android應用程序連接,直通ClientSocket的。這樣你可以序列化數據並通過流發送。並在服務器端反序列化並重建對象。這種方法需要更有紀律的軟件開發。如果你是第一次開發者,可能會很麻煩,我會推薦選項1.

希望我已經給出了一些基本的方向。

1

我注意到幾個問題(可能是拼寫錯誤,但我更喜歡問反正):

  • 要調用,但是你有沒有包括在這個問題它的源代碼 - 你必須這樣的服務,對吧? :)
  • onContextItemSelected您開始UpdateActivity並通過UserID額外。但是在該活動的showInfo方法中,您試圖從意圖的額外獲得AppointmentID,因此它不處理任何數據。
0

1)請渡過你的PHP代碼工作正常或不... 你可以做到這一點通過直接在服務器和傳球運行UserId和appoinmentId作爲參數?

對於如.... www.abc.com?sUserId=123?sAppointmentID=456

看天氣會向您顯示合適的輸出。

2)你正在調用getByMemberID.php,但你已經指定了它的代碼,但你沒有通過「AppointmentID」,因爲這也檢查與PHP代碼天氣你正在尋找?

3)對UpdateActivity.java updateData.php召喚:您沒有提供「AppointmentID」但你是在PHP代碼retriving它,它會得到默認空值,這將是ofcourse