--asp.net web service
using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Web.Services.Description;
using Resin;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using Jayrock.Json;
using Jayrock.JsonRpc;
using Jayrock.JsonRpc.Web;
using Jayrock.Configuration;
using Jayrock.Json.Conversion;
/// <summary>
///AndroidService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class AndroidService : System.Web.Services.WebService
{
private static JavaScriptSerializer serializer = new JavaScriptSerializer();
public AndroidService()
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod]
public string getArray() { return serializer.Serialize(new string[] { "A", "B", "c" }); }
[WebMethod]
public string getObject() { var json = new { RS = "OK" }; return serializer.Serialize(json); }
/// <SUMMARY>
/// Client 端傳 JSON 格式, Server 端解
/// </SUMMARY>
///
[WebMethod]
public string setObject(string json)
{
JsonObject obj = JsonConvert.Import(typeof(JsonObject), json) as JsonObject;
string name;
string age;
try {
name = obj["name"].ToString();
age = obj["age"].ToString();
}
catch (Exception) {
throw;
}
var rs = new{RS = "OK" ,name = name,age = age};
return serializer.Serialize(rs);
}
[WebMethod]
public string Add(int x, int y)
{
int z = x + y; return z.ToString();
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string Login(string UserName, string Pwd)
{
string returnUrl;
string loginname = UserName.Trim().Replace("-", "_");//防注入
string pws = Pwd.Trim().Replace("-", "_");
string mac = "";
//如果当前用户是mac认证用户txtMAC
string CliectIP = CommonFunction.GetIP();
string ClientArea = CommonFunction.GetClientArea(CliectIP);
SqlDataReader dr = UserBase.LoginCheck(loginname, pws, CliectIP, mac);
if (dr.Read())// a.LoginName,a.LimitString,a.EmployeeID,a.ID,b.dmmc
{
string Loginname = dr.GetString(0).Trim();
switch (Loginname)
{
case "ERROR":
{
//MessageBox.Show(this, "密码或用户名错误,请重新输入,如果您忘记了密码,请点击[忘记密码]使用手机找回密码!");
CommonFunction.USP_Insert_SYS_OperateLog(loginname, "登陆", "登陆失败", CliectIP, "密码错误," + pws + ",登陆地区:" + ClientArea);
break;
}
case "LOCK":
{
//MessageBox.Show(this, "当前用户已经被锁定,请联系管理员问明原因!");
CommonFunction.USP_Insert_SYS_OperateLog(loginname, "登陆", "用户锁定失败", CliectIP, "用户被锁定" + pws + ",登陆地区:" + ClientArea);
break;
}
case "Expiration":
{
//MessageBox.Show(this, "您已经超过系统设定的最长登陆期限,请联系管理员延长登陆期限!");
CommonFunction.USP_Insert_SYS_OperateLog(loginname, "登陆", "登陆期限过期", CliectIP, "登陆期限" + pws + ",登陆地区:" + ClientArea);
break;
}
case "LAN":
{
//MessageBox.Show(this, "当前帐号只可内部网登陆,请联系管理员解除限制!");
CommonFunction.USP_Insert_SYS_OperateLog(loginname, "登陆", "内网登陆限制", CliectIP, "内网登陆限制" + pws + ",登陆地区:" + ClientArea);
break;
}
case "MAC":
{
//MessageBox.Show(this, "当前帐号已设定固定计算机访问限制,不能访问!");
CommonFunction.USP_Insert_SYS_OperateLog(loginname, "登陆", "计算机限制", CliectIP, "计算机限制:" + mac + ",登陆地区:" + ClientArea);
break;
}
default:
{
string userdm = dr["ID"].ToString().Trim();
break;
}
}
dr.Close();
}
else
{
//MessageBox.Show(this, "您输入的校验码不正确,请检查后重新输入!");
}
//return new JavaScriptSerializer().Serialize();
return "false";
}
}
--端android
import java.util.ArrayList;
import java.util.List;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class EPersonTrackActivity extends Activity {
/** Called when the activity is first created. */
private static final String NAMESPACE = "http://tempuri.org/";
// asmx 路徑
private static final String URL = "http://180.110.246.98:8888/resin/josnservice/AndroidService.asmx";
//要呼叫的方法
private static final String MemberLogin_SOAP_ACTION = NAMESPACE+"setObject";
private static final String METHOD_NAME2 = "setObject";
private Button btnLogin=null;
private TextView labelInfo=null;
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(resin.Common.R.layout.main);
btnLogin=(Button)(findViewById(resin.Common.R.id.btnLogin));
labelInfo=(TextView)(findViewById(resin.Common.R.id.labelInfo));
btnLogin.setOnClickListener(new Button.OnClickListener(){
public void onClick(final View arg0) {
// TODO Auto-generated method stub
//返回json
String method = "setObject";//方法名称
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
JSONObject json = new JSONObject();
try {
json.put("name", "milkmidi");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
json.put("age", "30");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String rs = "";
params.add(new BasicNameValuePair("json", json.toString()));
//params.add(new BasicNameValuePair("y", String.valueOf(10)));
Context mContext =EPersonTrackActivity.this;
String wsurl=mContext.getString(resin.Common.R.string.wsurl);
rs =WSHelper.GetResponse(method,params,wsurl);
labelInfo.setText(rs);
//JSONObject rsJson = new JSONObject(rs);
//return rsJson.getString("name")+","+rsJson.getString("age");
/*
返回单值
String method = "Add";//方法名称
List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
params.add(new BasicNameValuePair("x", String.valueOf(5)));
params.add(new BasicNameValuePair("y", String.valueOf(10)));
Context mContext =EPersonTrackActivity.this;
String wsurl=mContext.getString(resin.Common.R.string.wsurl);
labelInfo.setText(WSHelper.GetResponse(method,params,wsurl));
try {
//mMsgTxt.setText( doSetObject() );
labelInfo.setText(doSetObject());
}
catch (JSONException e)
{
e.printStackTrace();
}
*/
}
});
}
public String doSetObject()
throws JSONException
{
Log.i("[WebService]", "doSetObject");
String rs = "";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME2);
JSONObject json = new JSONObject();
json.put("name", "milkmidi");
json.put("age", "30");
request.addProperty("json", json.toString());
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true; envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
try {
androidHttpTransport.call(MemberLogin_SOAP_ACTION, envelope);
SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
rs = result.toString();
}
catch (Exception e)
{
TextView mMsgTxt=(TextView)(findViewById(resin.Common.R.id.mMsgTxt));
mMsgTxt.setText(e.toString());
}
JSONObject rsJson = new JSONObject(rs);
return rsJson.getString("name")+","+rsJson.getString("age");
}
}
//通用调用ws方法
package resin.Common;
import java.util.List;
import org.apache.http.message.BasicNameValuePair;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import android.content.Context;
public class WSHelper
{
private static String namespace = "http://tempuri.org/";
/*************************************
* * 获取web services内容
* * @param url
* * @param params
* * @return
* *************************************/
public static String GetResponse(String method,List<BasicNameValuePair> params,String wsurl)
{
try {
String WSUrl=wsurl;//; //this.getResources().getString(R.string.wsurl);
//application.getString(R.string.wsurl);
String url = WSUrl;
SoapObject request = new SoapObject(namespace, method);
for(int i=0,len=params.size();i<len;i++){
request.addProperty(params.get(i).getName(),
params.get(i).getValue());
}
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(url);
androidHttpTransport.call(namespace + method, envelope);
SoapPrimitive result = (SoapPrimitive)envelope.getResponse();
return result.toString();
}
catch (Exception e)
{
return "Error:调用web services出错!"+e.getMessage();
}
}
}