Request Error when requesting a WCF RESTful service from Android 2.2 phone
I 开发者_运维百科wanted to create a method in Android that could communicate with a WCF RESTful service by giving it a string parameter.
However, whenever I make the request all I get in return is a "Request Error" error. I've tried calling the service via Fiddler, without giving it any parameters, and it works just fine.
What am I doing here, and how can I fix it?
Thanks for your time!
Here's the code for the Java method:
private void addNewSighting() throws Exception
{
String URL = "http://liveandesor.web711.discountasp.net/wcf/SightingServiceRest.svc/json/SaveNewSightingRest";
AlertDialog popup;
SightingWrapper sighting = new SightingWrapper();
String xml = createXML(sighting);
try{
HttpPost request = new HttpPost(URL);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
JSONObject json = new JSONObject();
json.put("sighting", "holi");
StringEntity sen = new StringEntity( "JSON: " + json.toString());
sen.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
request.setEntity(sen);
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(request);
HttpEntity responseEntity = response.getEntity();
char[] buffer = new char[(int)responseEntity.getContentLength()];
InputStream stream = responseEntity.getContent();
InputStreamReader reader = new InputStreamReader(stream);
reader.read(buffer);
stream.close();
String responseString = new String(buffer);
String holi = responseString;
}
catch(Exception e)
{
}
}
Here's the contract for the method I want to call, which is located in the SightingServiceRest service and only returns the string "IT'S ALIVE!!":
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "SaveNewSightingRest", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
string SaveNewSightingRest(string sighting);
Here's the webconfig for the WCF project:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="LiveAndesWCF" connectionString="data source=tcp:sql2k802.discountasp.net;Initial Catalog=SQL2008_832326_liveandes;User ID=randomUser;Password=randomPass; MultipleActiveResultSets=true" providerName="System.Data.SqlClient"/>
<add name="LiveAndes" connectionString="data source=tcp:sql2k802.discountasp.net;Initial Catalog=SQL2008_832326_liveandes;User ID=randomUser;Password=randomPass; MultipleActiveResultSets=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="LiveAndes" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
</providers>
</membership>
</system.web>
<appSettings>
<!--Local-->
<!--
<add key="mainPathDeployWCF" value="http://localhost:61642/"/>
<add key="mainMachPathDeployWCF" value="\LiveAndesWCF\bin"/>
-->
<!-- Deploy -->
<add key="mainPathDeployWCF" value="http://liveandesor.web711.discountasp.net/"/>
<add key="mainMachPathDeployWCF" value="e:\web\liveandesor\htdocs"/>
<!--<add key="mainMachPath" value="C:\Users\Rul\Documents\Universidad\2011'1\Taller de Especialidad\svn\Desarrollo\WEB\LiveAndesMVC\LiveAndesMVC"/>-->
</appSettings>
<system.serviceModel>
<protocolMapping>
<add scheme="http" binding="wsHttpBinding" />
</protocolMapping>
<services>
<service name="LiveAndesWCF.SightingService">
<endpoint address="soap" binding="basicHttpBinding" contract="LiveAndesWCF.ISightingService"/>
<endpoint address="json" binding="webHttpBinding" behaviorConfiguration="jsonBehavior" contract="LiveAndesWCF.ISightingService"/>
</service>
<service name="LiveAndesWCF.UserService">
<endpoint address="soap" binding="basicHttpBinding" contract="LiveAndesWCF.IUserService"/>
<endpoint address="json" binding="webHttpBinding" behaviorConfiguration="jsonBehavior" contract="LiveAndesWCF.IUserService"/>
</service>
<service name="LiveAndesWCF.UserServiceRest" behaviorConfiguration="MetadataBehavior">
<endpoint address="json" behaviorConfiguration="WebBehavior" binding="webHttpBinding" contract="LiveAndesWCF.IUserServiceRest"/>
</service>
<service name="LiveAndesWCF.SightingServiceRest" behaviorConfiguration="MetadataBehavior">
<endpoint address="json" behaviorConfiguration="WebBehavior" binding="webHttpBinding" contract="LiveAndesWCF.ISightingServiceRest"/>
</service>
<service name="LiveAndesWCF.TestService">
<endpoint address="soap" binding="basicHttpBinding" contract="LiveAndesWCF.ITestService"/>
<endpoint address="json" binding="webHttpBinding" behaviorConfiguration="jsonBehavior" contract="LiveAndesWCF.ITestService"/>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding maxReceivedMessageSize="99999999" closeTimeout="00:02:00" openTimeout="00:02:00" receiveTimeout="00:10:00" sendTimeout="00:02:00">
<readerQuotas maxArrayLength="76384000" maxStringContentLength="2147483647"/>
</binding>
</basicHttpBinding>
<webHttpBinding>
<binding maxReceivedMessageSize="99999999" closeTimeout="00:02:00" openTimeout="00:02:00" receiveTimeout="00:10:00" sendTimeout="00:02:00">
<readerQuotas maxArrayLength="76384000" maxStringContentLength="2147483647"/>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="jsonBehavior">
<enableWebScript/>
<!--<webHttp/>-->
</behavior>
<behavior name="WebBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
<behavior name="MetadataBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
The prefix JSON:
is not correct. You hardly used it in your test with Fiddler.
Replace that line with:
StringEntity sen = new StringEntity(json.toString());
精彩评论