I am trying to post data to my server (vb.net) using mobile devices. I've researched several great articles on the client-side (Android) including this Encosia article and sending JSON object with Android.
I have created a sample class:
Public Class OneEvaluation
Private strEmail As String
Public Property email() As String
Get
Return strEmail
End Get
Set(ByVal value As String)
strEmail = value
End Set
End Property
Private strPassword As String
Public Property password() As String
Get
Return strPassword
End Get
Set(ByVal value As String)
strPassword = value
End Set
End Property
End Class
and I've created my webmethod:
Public Class AsmxCodebehind
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function AndroidTest(ByVal JSON As OneEvaluation) As String '
Dim strSQLInsertCommand As String
' code block
Return "whatever"
End Function
It builds (VS2008 sp1) with no errors. I get no response on the android.
If I delete the argument 'JSON as OneEvaluation', it will successfully post in a browser and provide a HTML return; but as coded above... the error is:
System.InvalidOperationException: AndroidTest Web Service method name is not valid.
at System.Web.Services.Protocols.HttpServerProtocol.Initialize()
FYI, my Android snippet follows:
final String URL = "http://www3.myurl.com/JSON/service.asmx/AndroidTest"
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
HttpResponse response;
JSONObject json = new JSONObject();
try{
HttpPost post = new HttpPost(URL);
json.put("email", email);
json.put("password", pwd);
StringEntity se = new StringEntity( "OneEvaluation: " + json.toString());
// post.setEntity(se);
post.setHeader("Accept", "application/json");
post.setHeader("Content-type", "application/json; charset=utf-8");
// se.setContentEncoding((Header) new BasicHeader(HTTP.CONTENT_TYPE, "application/json; charset=utf-8"));
post.setEntity(new ByteArrayEntity(se.toString().getBytes("UTF8")));
// post.setHeader(OneEvaluation, value)
// ResponseHandler responseHandler = new BasicResponseHandler();
response = client.execute(post);
/* Checking response */
Log.v("TAG", " JSON onItemClick fired! Position:");
if(response!=null){
Log.v("TAG", " Response :" + response.toString());
// Toast.makeText(v.getContext(), response, Toast.LENGTH_LONG);
InputStream in = response.getEntity().getContent(); //Get the data in the entity
}
}
catch(Exception e){
e.printStackTrace();
createDialog("Error", "Cannot Estabilish Connection");
Log.v("TAG", " Cannot Estabilish Connection");
}
I haven't seen a complete example with both server-side (except those using jQuery) and client side (especially Android). So I hope to save someone else's weekend!
I have resisted using C# on the server side, as my other AJAX/JSON services (including those that return JSO开发者_如何学GoN) are in vb (and working).
I'm asking for help with the server side error... and any suggestions to clean up the Android JSON HTTP_POST.
Many Thanks in advance !
Whew! I couldn't have done it without Fiddler and some GREAT posts, like this answer from @Mark-Schultheiss.
Also, future travelers... beware that the correct data format for your JSON is expected!
{"JSON": {"email":"kiddin","password":"no"}}
was the only way it was accepted. Single Quot is unacceptable... so escape a double quote (\") into the code and call the methodname (ie JSON) - not the class.
I couldn't have done it without testing using a webpage using jQuery. I recommend this blog... http://encosia.com/3-mistakes-to-avoid-when-using-jquery-with-aspnet-ajax/'> Encosia from @Dave-Ward. Just remember, I had to use double quotes rather than the mentioned single quote.
精彩评论