我正嘗試使用移動設備將數據發佈到我的服務器(vb.net)。 我在客戶端(Android)上研究了幾篇很棒的文章,包括Encosia article和sending JSON object with Android。vb.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的任何建議。
很多預先感謝!
我仍然努力得到這個在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