我需要将数据(字符串)从Android客户端传输到WCF服务。但我无法做到 - Android-client访问服务方法,但字符串未传递,它始终为空。但PC可以访问要记录的文件。
我有一个WCF服务,可以与Android应用程序进行通信。我将服务放在IIS中,并在Android Studio中使用Android模拟器。托管的WCF-Service已经发布是正确的,至少将数据传输到Android应用程序是好的
Android-app转移如下:
private class ConWCF extends AsyncTask<Void, Void, String>{protected String doInBackground(Void... params){
InputStream inputStream = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://192.168.1.94:8080/Test.svc/Login");
String json = "";
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("firstname", "Alex");
jsonObject.put("secondName", "Petrov");
jsonObject.put("street", "Arbat");
} catch (JSONException e) {
e.printStackTrace();
}
json = jsonObject.toString();
StringEntity se = new StringEntity(json);
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse httpResponse = httpclient.execute(httpPost);
inputStream = httpResponse.getEntity().getContent();
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
return result;}}
Wcf-service使用以下代码获取json-lines:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "Login",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json
)]
void Login(String str);
public void Login(String str){
var filePath = ConfigurationManager.AppSettings["myFile.txt"];
StreamWriter file;
file = File.AppendText(filePath + "/myFile.txt");
if (str == null)
{
file.WriteLine("пусто");
}
else
{
file.WriteLine(str);
}
file.WriteLine(str);
file.Close();
}
在Web.config中,我还设置了我想要记录的文件的路径。
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /><add key="myFile.txt" value="C:\Papka" />
当我使用WPF-client检查没有Android应用程序的记录时 - 记录正确运行。 Android-clien拥有允许使用互联网的所有清单