[英]How do I send a request to get specific data from server using HttpUrlConection in android?
[英]How to get data from Server using ID in Android
我正在編寫一個本機Android應用程序,其中使用PHP MYSQL從服務器獲取數據
在此[約會列表]中,我允許用戶重新安排約會,但是每當我點擊獲取空白表格的項目時,簡而言之,不會獲得我在列表中單擊的特定約會的數據。
題
如何使用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();
}
}
選項1
開發Java EE Web服務,並將其托管在服務器上。 您的Android應用需要將數據與Web服務相關聯,然后將其發送到服務器。 服務器可以接收數據並使用它來做自己想做的事情。 服務器還可以通過Web服務的相同響應來處理和響應更多信息。 您需要通過Google搜索世界Java EE。 我找到了一個。
Java EE在本地台式機(或開發機)上運行后,您可以將WAR或EAR復制到EC2,以使應用程序可以通過Internet運行,而不必費心創建開放的網絡。 DMZ。
選項2
您可以開發基於Java套接字的產品,您的android應用程序可以通過clientsocket連接到該產品。 這樣,您可以序列化數據並通過流發送它。 然后在服務器端反序列化並重建對象。 這種方法需要更嚴格的軟件開發。 如果您是第一次開發人員,可能會遇到很多問題,我建議您選擇選項1。
希望我給出一些基本的指示。
首先,請確保您具有有效的AppointmentID
和UserId
以及
在UpcomingActivity.java
的方法onContextItemSelected
您沒有提供AppointmentID
代替你只提供UserID
的Intend
列出但在方法showData
Updateactivity
,您正在請求被請求的intent.getStringExtra("AppointmentID")
。
因此您的更新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;
}
我注意到了幾個問題(可能是拼寫錯誤,但無論如何我還是想問):
getByMemberID.php
,但是您沒有在問題中包含其源代碼-您確實有這樣的服務,對嗎? :) onContextItemSelected
您將啟動UpdateActivity
並額外傳遞UserID
。 但是,在該活動的showInfo
方法中,您嘗試從意圖的附加項中獲取AppointmentID
,因此它不處理任何數據。 1)嘗試在您的PHP代碼運行正常與否的情況下進行操作...您可以通過直接在服務器上運行並將UserId和appoinmentId作為參數傳遞來完成此操作
例如... www.abc.com?sUserId=123?sAppointmentID=456
查看天氣,它會向您顯示適當的輸出。
2)您正在調用getByMemberID.php,但是您已經指定了它的代碼,但是您沒有通過“ AppointmentID”,因此也請使用php代碼檢查您是否正在檢索它?
3)在UpdateActivity.java中調用updateData.php時:您未提供“ AppointmentID”,但您正在php代碼中檢索它,默認情況下它將獲得null值,這當然是錯誤的
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.