【发布时间】:2016-01-22 21:56:14
【问题描述】:
我正在尝试开发一个项目,其中我的 android 应用程序将 IP 地址和端口号发送到 wcf 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;
}
代码安卓
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;
}
【问题讨论】:
-
您不需要任何 json 对象,只需发出类似
http://algotimesheet.azurewebsites.net/Service1.svc/Connection/192.168.1.1/4370的“GET”请求(不像您尝试“POST”) -
以及如何发送请求?