我有一个的Windows Web服务器
已经建立了一个网站(无限的应用程序池
),我想能够通过访问
我开发。我怎样才能做到这一点?可有人点我一个教程或给$ C的$ C例子,说明这个跨平台(数据库
该服务器上的Android应用程序的Android / Java的
到 ASP.NET/C#
)的通信可以做什么?
I have a Windows web server
already set up with a website (unlimited application pools
) and I want to be able to access a database
on that server via the Android app
I'm developing. How can I do this? Can someone point me to a tutorial or give code example of how this cross-platform (Android/Java
to ASP.NET/C#
) communication can be done?
(我试图建立一个领导委员会或全球记分牌我的服务器上我的安卓
游戏。)
(I'm trying to create a leader board or global scoreboard for my Android
game on my server.)
感谢。
您的应用程序应该公开一个Web服务。 没有用于.NET皂基Web服务的原生支持。但是你可以使用KSOAP的Android端口:
Your app should expose a webservice. There is no native support for .net soap based webservices. But you can use the ksoap android port:
HTTP://$c$c.google.com/p/ksoap2-android/
这使得Android应用程序消耗.NET Web服务的ASMX。 但是在客户端复杂的deserialisation涉及很多code写的每一个对象,你要那么传递给客户端。
which allows an android app to consume a .net asmx webservice. However the deserialisation of complex on the client side involves lot of code writing for every object you want so pass to the client.
我尝试了一个项目,并出现了一些问题,我碰到了(或者我可以得到结果返回给客户端,但我传递的参数那里总是空或者其他的方式 - 我可以传递参数,但结果却是零)
I tried it for a project and there were some problems I ran into (either I could get result back to the client but the parameters i passed where always null or the other way - I could pass arguments but the result was null).
下面是一个例子,我张贴得到一个int:http://stackoverflow.com/questions/1052300/how-to-call-a-net-webservice-from-android-using-ksoap2/1521287#1521287
Here is an example I posted for getting an int: http://stackoverflow.com/questions/1052300/how-to-call-a-net-webservice-from-android-using-ksoap2/1521287#1521287
不过,从我目前的knowlege我会建议使用的.asmx web服务,返回一个JSON字符串,并使用Java JSON串行器解析输出。优点:
However, from my current knowlege I would suggest using a .asmx webservice that returns a json string and use a java json serialiser to parse the output. The advantages:
在编写更少code 更快,因为移动设备不总是有良好的互联网连接和肥皂的XML开销大于JSON。快速入门:
在您的.NET Web应用程序创建一个新的ASMX web服务。 在其中提及的System.Web。装饰你的web服务类[ScriptService],你的方法[ScriptMethod(ResponseFormat = ResponseFormat.Json)] Create a new asmx Webservice in your .net webapp. Include a reference to System.Web.
Decorate your webservice class with [ScriptService] and your method with [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloAndroid()
{
return "Hello Android";
}
}
(我认为你必须添加一个引用到System.Web.Extension.dll它可因为.NET 3.5)。
(I think you have to add a reference to System.Web.Extension.dll which is available since .net 3.5).
您的web服务仍然会返回XML(这样你就可以使用它与SOAP客户端),除非你与内容类型应用/ JSON的一个HTTPPost请求。
Your webservice will still return XML (so you can use it with a soap client) unless you make a HTTPPost request with content-type "application/json".
使用code联系,从机器人的web服务:
use this code to contact the webservice from android:
private JSONObject sendJsonRequest(string host, int port,
String uri, JSONObject param)
throws ClientProtocolException, IOException, JSONException
{
HttpClient httpClient = new DefaultHttpClient();
HttpHost httpHost = new HttpHost(host, port);
HttpPost httpPost = new HttpPost(uri);
httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
if (param != null)
{
HttpEntity bodyEntity = new StringEntity(param.toString(), "utf8");
httpPost.setEntity(bodyEntity);
}
HttpResponse response = httpClient.execute(httpHost, httpPost);
HttpEntity entity = response.getEntity();
String result = null;
if (entity != null) {
InputStream instream = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(instream));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
sb.append(line + "\n");
result = sb.toString();
instream.close();
}
httpPost.abort();
return result != null ? new JSONObject(result) : null;
}
}
如果您的Web服务的方法是这样的:
if your webservice methods looks like this:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public User GetUser(string name, int age)
{
return new User { Name = name, Age = age; }
}
您可以把它叫做这样的机器人:
You can call it this way from android:
public void getUser() {
// if you put a json object to the server
// the properties are automagically mapped to the methods' input parameters
JSONObject param = new JSONObject();
param.put("name", "John Doe");
param.put("age", 47);
JSONObject result = sendJsonRequest("server", 80,
"http://server:80/service1.asmx/GetUser", param);
if (result != null) {
JSONObject user = new JSONObject(result.getString("d"));
// .net webservices always return the result
// wrapped in a parameter named "d"
system.out.println(user.getString("name"));
system.out.println(user.getInt("age").toString());
}
}
}
在客户端处理服务器的异常:
Handling server exceptions on the client side:
将此类项目:
Add this class to your project:
import org.json.JSONException;
import org.json.JSONObject;
public class JSONExceptionHelper {
private static final String KEY_MESSAGE = "Message";
private static final String KEY_EXCEPTIONTYPE = "ExceptionType";
private static final String KEY_STACKTRACE = "StackTrace";
public static boolean isException(JSONObject json) {
return json == null
? false
: json.has(KEY_MESSAGE) &&
json.has(KEY_EXCEPTIONTYPE) &&
json.has(KEY_STACKTRACE);
}
public static void ThrowJsonException(JSONObject json) throws JSONException {
String message = json.getString(KEY_MESSAGE);
String exceptiontype = json.getString(KEY_EXCEPTIONTYPE);
String stacktrace = json.getString(KEY_STACKTRACE);
StringBuilder sb = new StringBuilder();
sb.append(exceptiontype);
sb.append(": ");
sb.append(message);
sb.append(System.getProperty("line.separator"));
sb.append(stacktrace);
throw new JSONException(sb.toString());
}
}
}
现在从sendJSONRequest替换return语句:
Now replace the return statement from the sendJSONRequest with:
JSONObject json = result != null ? new JSONObject(result) : null
if (JSONExceptionHelper.isException(json))
JSONExceptionHelper.ThrowJsonException(json);
return json;
请注意:唯一的例外是传递给客户端仅当连接来自本地主机。 否则,你会得到一个HTTP错误500(或501?我不记得了)。你必须配置IIS以发送错误500到客户端。
Please note: The exception is passed to the client only if connection comes from localhost. Otherwise you get an http error 500 (or 501? I can't remember). You have to configure your IIS to send error 500 to the client.
尝试,并且创造一个web服务总是抛出异常。
Try it out and create a webservice that always throws an exception.
上一篇:使用twitter4j Android的微博XAUTH例子例子、twitter4j、Android、XAUTH
下一篇:这款Android SDK需要ADT版本23.0.0或更高版本。现在的版本是22.6。请更新ADT到最新版本?版本、这款、最新版本、或更高