1
我創建了一個由android使用的WCF服務。它在json中返回DataRows。至於,Rest的wcf服務在json中返回數據行 - android客戶端
的Web.config
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="httpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="WcfAndroid.Service1">
<endpoint address=""
behaviorConfiguration="httpBehavior"
binding="webHttpBinding"
contract="WcfAndroid.IService1" />
</service>
</services>
<protocolMapping>
<add binding="webHttpBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
服務接口IService1:
<OperationContract()> _
<WebInvoke(Method:="POST", UriTemplate:="GetEmp", ResponseFormat:=WebMessageFormat.Json)> _
Function GetEmp(ByVal EmpId As String) As DataTable
服務類服務:
Public Function GetEmp(ByVal EmpId As String) As DataTable
Dim table As New DataTable("mytable")
table.Columns.Add("Result", GetType(String))
table.Columns.Add("acc", GetType(String))
table.Columns.Add("name", GetType(String))
table.Columns.Add("paid", GetType(Double))
'' Using EmpId for Fetching data from Database
table.Rows.Add("True", "1", "Employee1", 5000)
table.Rows.Add("True", "2", "Employee2", 2000)
Return table
End Function
EMPID是爲每一位員工的唯一ID。我從Sql Server數據庫中獲得詳細信息。爲了測試,我手動發送了2行。
在Android應用程序WebGet我已經使用:
public static JSONObject requestWebService(String serviceUrl) {
HttpURLConnection urlConnection = null;
try {
URL urlToRequest = new URL(serviceUrl);
urlConnection = (HttpURLConnection)
urlToRequest.openConnection();
int statusCode = urlConnection.getResponseCode();
if (statusCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
// handle unauthorized
} else if (statusCode != HttpURLConnection.HTTP_OK) {
// handle any other errors
}
InputStream in = new BufferedInputStream(
urlConnection.getInputStream());
String ss=getResponseText(in);
return new JSONObject(ss);
} catch (MalformedURLException e) {
// URL is invalid
} catch (SocketTimeoutException e) {
// data retrieval or connection timed out
} catch (IOException e) {
// could not read response body
// (could not create input stream)
} catch (JSONException e) {
// response body is no valid JSON string
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return null;
}
但我不知道如何與EMPID和getDataRows自檢。
我必須在Android應用程序中使用此服務。我正在使用HttpURLConnection。
如何使用HttpURLConnection和getDatarows(json格式)post?