2011-07-18 75 views
3

我正嘗試使用移動設備將數據發佈到我的服務器(vb.net)。 我在客戶端(Android)上研究了幾篇很棒的文章,包括Encosia articlesending JSON object with Androidvb.net asmx WebService使用Android在HTTP-POST中接受JSON

我創建了一個示例類:

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 

,我已經建立了我的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 

它建立(VS2008 SP1)沒有任何錯誤。我在android上沒有任何反應。 如果我刪除參數'JSON作爲OneEvaluation',它將成功地在瀏覽器中發佈並提供HTML返回;但正如上面編碼...的錯誤是: System.InvalidOperationException: AndroidTest Web Service method name is not valid. at System.Web.Services.Protocols.HttpServerProtocol.Initialize()

僅供參考,我的Android片斷如下:

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"); 
} 

我還沒有看到有兩個服務器端的一個完整的示例(除使用jQuery的)和客戶端(特別是Android)。所以我希望能拯救別人的週末!

我拒絕在服務器端使用C#,因爲我的其他AJAX/JSON服務(包括那些返回JSON)在vb(和工作)。

我在尋求服務器端錯誤的幫助......以及清理Android JSON HTTP_POST的任何建議。

很多預先感謝!

+0

我仍然努力得到這個在IIS中觸發我已經使用Fiddler來確認我在POST中發送JSON Fiddler顯示我的帖子爲: 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

回答

2

Whe!如果沒有Fiddler和一些偉大的帖子,來自@ Mark-Schultheiss的 like this answer,我是無法做到的。

此外,未來的旅行者......請注意您的JSON的正確數據格式預計!

{"JSON": {"email":"kiddin","password":"no"}}是它被接受的唯一方法。單一的報價是不可接受的......所以在代碼中加入雙引號(\「),然後調用方法名稱(即JSON) - 不是類別。 。我推薦這個博客... http://encosia.com/3-mistakes-to-avoid-when-using-jquery-with-aspnet-ajax/'>來自@ Dave-Ward的Encosia。請記住,我不得不使用雙引號,而不是提到的單引號