我必须将一些数据发送/发布到基本连接到远程数据库的.svc
Web服务。我正在使用JSONStringer
发送数据,但每次响应状态为false。我的数据没有发送。如何在Android中使用HttpPost
。有人可以帮我解决这个问题。
这是我的网络服务代码
String namespace = "http://103.24.4.60/xxxxx/MobileService.svc";
public void ActivityUpload( final String strCurrentDateTime, final String strTitle, final String replaceDescChar, final String editedHashTag)
{
new AsyncTask<String, Void, String>()
{
@Override
protected String doInBackground(String... arg0)
{
String line = "";
try
{
Log.e("ActionDate "," = "+ strCurrentDateTime);
Log.e("ActivityId"," = "+strActivityId);
Log.e("UserId"," = "+str_UserId);
Log.e("ObjectId"," = "+strVessId);
Log.e("Name"," = "+strTitle);
Log.e("Remark"," = "+replaceDescChar);
Log.e("Status"," = "+"PENDING");
Log.e("Type"," = "+strType);
Log.e("starflag"," = "+0);
Log.e("HashTag"," = "+editedHashTag);
Log.e("Authentication_Token"," = "+str_Authentication_Token);
// make web service connection
HttpPost request = new HttpPost(namespace + "/Upd_Post_Activity");
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
// Build JSON string
JSONStringer TestApp = new JSONStringer().object()
.key("ActionDate").value(strCurrentDateTime)
.key("ActivityId").value(strActivityId)
.key("UserId").value(str_UserId)
.key("ObjectId").value(strVessId)
.key("Name").value(strTitle)
.key("Remark").value(replaceDescChar)
.key("Status").value("PENDING")
.key("Type").value(strType)
.key("starflag").value("0")
.key("HashTag").value(editedHashTag)
.key("Authentication_Token").value(str_Authentication_Token).endObject();
StringEntity entity = new StringEntity(TestApp.toString());
Log.d("****Parameter Input****", "Testing:" + TestApp);
request.setEntity(entity);
// Send request to WCF service
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
Log.d("WebInvoke", "Saving: " + response.getStatusLine().toString());
// Get the status of web service
BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
// print status in log
while ((line = rd.readLine()) != null) {
Log.d("****Status Line***", "Webservice: " + line);
}
} catch (Exception e) {
e.printStackTrace();
}
return line;
}
}.execute();
}
这是输入参数。
****Parameter Input****﹕ Testing:{"ActionDate":"2016-01-21%2014:20:43%20PM","ActivityId":"120160119180421058","UserId":"125","ObjectId":"1","Name":"Title2","Remark":"Test%20two","Status":"PENDING","Type":"3","starflag":"0","HashTag":"990075","Authentication_Token":"6321D079-5B28-4F3F-AEE7-D59A1B9EFA59"}
先谢谢。
答案 0 :(得分:0)
意识到android httpclients正在弃用(支持httpsurlconnection)但是,这些httpclient仍然被广泛使用。关于gradle构建,考虑到deprication,并且对于小的依赖lib tweek,httpclient可能会使用一段时间。
(仍然会使用httpclient?)
将安卓放在一边。
完成后,您可以使用您选择的httpclient将curl测试的组件转移到android.httpclient.exec.POST。
2.A。确保客户端'request'构造函数的默认标题列表默认情况下不包含一些您不想要的标题...为了确保这一点,您可能需要为您的客户端启用HEADER日志记录... 。java example logger。删除POST的框架构造函数所包含的不必要的头文件。
2.b android logger(WIRE,HEADERS)是不同的,可能需要一些挖掘,取决于正在使用的客户端。
3.A。根据JSON lib,创建消息对象,然后将对象转换为某个友好类型,用于发布实体中的封装,即使用“writer”将对象转换为带有JSON的序列化字符串。
reqRole = new ObjectMapper().createObjectNode();
reqRole.put("__type", "Pointer");
reqRole.put("className", "_Role");
reqRole.put("objectId", roleId);
rootOb.put("requestedRole", reqRole);
rootOb.put("requestedBy",usersArr);
StringWriter writer = new StringWriter();
try {
new ObjectMapper().writeValue(writer, rootOb)
..
String http-post-str=writer.toString();
3.B。在POST请求中用json包装字符串......
httpPost.setEntity(new StringEntityHC4(http-post-str));