我对android开发还是相当陌生的,并且正在使用REST服务来允许服务器上的应用程序和数据库之间进行通信。
我一直在开发一个应用程序,该应用程序提取与我们系统上记录的票证有关的信息。我的android应用程序能够成功使用REST服务从数据库中获取数据,但是我要让REST服务从我的应用程序接受JSON格式的数据来更新现有票证或创建新票证确实很难。
我的REST服务用C#编写
下面是我的GET请求的示例,由于有一些参数并且具有URL接受的长度,因此可以很好地工作:
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "incidentAgent/{agent}/{incID}")] //PASS A 0 incID IF SELECTING ALL
Stream AgentIncidentListing(string agent, string incID);
这是我的Android应用程序代码,用于生成JSON字符串并将POST请求发送到我的Web服务:
public void updateIncidents(View v){
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
//Declare and initialize text views from the layout
TextView tvidIncidents = (TextView) findViewById(R.id.tvIncidentID);
TextView tvResolution = (TextView)findViewById(R.id.tvResolution );
TextView tvJobCardNo = (TextView)findViewById(R.id.tvJobCardNo );
TextView tvActualHours = (TextView)findViewById(R.id.tvActualHours );
TextView tvBudgetHours = (TextView)findViewById(R.id.tvBudgetHours );
TextView tvTaskDesc = (TextView)findViewById(R.id.tvTaskDesc );
TextView tvOutline = (TextView)findViewById(R.id.tvOutline );
TextView tvProject = (TextView)findViewById(R.id.tvProject );
TextView tvIncidentType = (TextView)findViewById(R.id.tvIncidentType );
TextView tvStatus = (TextView)findViewById(R.id.tvStatus );
TextView tvCustomer = (TextView)findViewById(R.id.tvCustomer );
TextView tvIncidentNumber = (TextView)findViewById(R.id.tvIncidentNumber );
TextView tvDueBy = (TextView)findViewById(R.id.tvDueDate);
URL url = new URL(updateURI);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
conn.setRequestProperty("Accept","application/json");
conn.setDoOutput(true);
conn.setDoInput(true);
JSONObject jsonParam = new JSONObject();
jsonParam.put("iIncidentStatusID", tvStatus.getText());
jsonParam.put("iDebtorID", tvCustomer.getText());
jsonParam.put("cOutline", tvOutline.getText());
jsonParam.put("iIncidentTypeID", tvIncidentType.getText());
jsonParam.put("iProjectID", tvProject.getText());
jsonParam.put("ufINCBHours", tvBudgetHours.getText());
jsonParam.put("ufINCAH", tvActualHours.getText());
jsonParam.put("ucINCJN", tvJobCardNo.getText());
jsonParam.put("ucINCTaskDescription", tvTaskDesc.getText());
jsonParam.put("ucINCResolution", tvResolution.getText());
jsonParam.put("dDueBy", tvDueBy.getText());
jsonParam.put("idIncidents", tvidIncidents.getText());
Log.i("JSON", jsonParam.toString());
DataOutputStream os = new DataOutputStream(conn.getOutputStream());
//os.writeBytes(URLEncoder.encode(jsonParam.toString(), "UTF-8"));
os.writeBytes(jsonParam.toString());
os.flush();
os.close();
Log.i("STATUS", String.valueOf(conn.getResponseCode()));
Log.i("MSG" , conn.getResponseMessage());
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
我遇到的问题是,我完全不知道如何在我的REST服务上处理此POST请求。这是我到目前为止的内容:
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "updateIncData")]
void UpdateJsonIncident(RequestData rData);
RequestData类:
public class RequestData
{
[DataMember]
public string details { get; set; }
}
我希望能够读取我的Android应用返回的JSON字符串,并在UpdateJsonIncident()函数中将相关值分配给我的insert或update语句,以相应地影响我的数据库。
我已经尝试了很多东西,但是无法使它正常工作。在我的办公桌上坐了第二天(目前连续11个小时)后,我觉得在这里寻求帮助是我的最后选择,希望有人可以帮助我。
于是终于取得了突破。原来我要做的只是以下内容:
1)我创建了一个名为“ IncidentData”的类,如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;
namespace xxxxx.Classes
{
[DataContract]
public class IncidentData
{
[DataMember]
public string idIncidents { get; set; }
[DataMember]
public string IncidentStatus { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string cOurRef { get; set; }
[DataMember]
public string cOutline { get; set; }
[DataMember]
public string IncidentType { get; set; }
[DataMember]
public string Project { get; set; }
[DataMember]
public string BudgetedHours { get; set; }
[DataMember]
public string ActualHours { get; set; }
[DataMember]
public string JobCardNumber { get; set; }
[DataMember]
public string TaskDescription { get; set; }
[DataMember]
public string Resolution { get; set; }
[DataMember]
public string dDueBy { get; set; }
}
}
2)如下更改了我的运营合同:
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "/UpdateJsonIncident")]
bool UpdateJsonIncident(IncidentData incident);
3)现在,在UpdateJsonIncident函数中访问我的数据所需要做的一切如下:
public bool UpdateJsonIncident(IncidentData incident) {
string xxx = incident.cOutline // etc...
}
我希望这可以帮助别人节省很多时间,如果他们遇到相同的问题。