我正在尝试开发一个项目,其中我的android应用程序将一个IP地址和端口号发送到wcf Web服务: 但我无法做到 并且可以获得Web服务的响应??? IService.cs
[OperationContract]
[WebGet(UriTemplate = "Connection/{ipadress}/{port}")]
bool Connection(string ipadress,string port);
Service.svc.cs
public bool Connection(string ipadress, string port)
{
/* */
return true;
}
Android代码
public boolean Connection_Net() {
String url_service = "http://algotimesheet.azurewebsites.net/Service1.svc/Connection/ipadress/port";
URLConnection urlConnection;
DataOutputStream dataOutputStream;
DataInputStream dataInputStream;
URL url;
try {
url = new URL(url_service);
urlConnection = url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Host", "android.schoolportal.gr");
urlConnection.connect();
JSONObject jsonObject = new JSONObject();
jsonObject.put("ipadress","192.168.1.1");
jsonObject.put("port","4370");
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(urlConnection.getOutputStream());
outputStreamWriter.write(jsonObject.toString());
outputStreamWriter.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return true;
}
答案 0 :(得分:-1)
如果发送JSON,则需要编写服务以期望对象并使用POST
[OperationContract]
[WebInvoke(Method="POST",
RequestFormat = WebMessageFormat.Json,
UriTemplate = "Connection")]
bool Connection(Data data);
public bool Connection(Data data)
{
/* */
return true;
}
设计这样的对象:
public class Data()
{
public string ip {get; set;}
public string port{get;set;}
}
如果您想制作GET
,则只需将值添加到URL