【发布时间】:2020-08-08 22:39:09
【问题描述】:
我对 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 请求发送到我的网络服务:
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() 函数中的插入或更新语句,以相应地影响我的数据库。
我尝试了很多方法,但无法使其正常工作。在我的办公桌前坐了第二天(目前连续 11 小时)后,我觉得在这里请求帮助是我最后的选择,希望有人能帮助我。
【问题讨论】: