3

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 JSON) 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 !

Community
  • 1
  • 1
SolarBlitz
  • 63
  • 1
  • 7
  • I'm still struggling to get this to fire in IIS. I've used Fiddler to confirm that I am sending JSON in the POST. Fiddler shows my post as: POST http://www3.myweb.com/JSON/swUpdate.asmx/SaveProcedureList HTTP/1.1 Accept: application/json Content-type: application/json; charset=utf-8 Content-Length: 44 Host: www3.tidewatertechnology.com Connection: Keep-Alive User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4) Expect: 100-Continue {"OneEvaluation": {"email":"kiddin","password":"no"}} – SolarBlitz Jul 18 '11 at 21:07

1 Answers1

2

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.

Community
  • 1
  • 1
SolarBlitz
  • 63
  • 1
  • 7