调用方式一
private HashMap<String, String> stringHashMap =new HashMap<String, String>();//不new时一直报空指针异常
//确定按钮
public void btnOk(View view) throws IOException, XmlPullParserException {
String names = nametex.getText().toString();
String pwds = passwordtex.getText().toString();
if (TextUtils.isEmpty(names) || TextUtils.isEmpty(pwds)) {
Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_LONG).show();
}else{
//注意这里不能直接调用 网络请求位置问题,不能在主进程发起http请求。
new Thread(requestpost).start();
}
}
在主方法中不能直接进行网络请求!!!!(这个坑我踩了-.-半天新手哈)
往下走
//调用request请求直接访问
Runnable requestpost = new Runnable() {
@Override
public void run() {
String names = nametex.getText().toString();
String pwds = passwordtex.getText().toString();
stringHashMap.put("username", names);
stringHashMap.put("password",pwds);
requestPost(stringHashMap);
}
};
requestPost请求服务端
//直接使用请求路径去访问 有风险
private void requestPost(HashMap<String, String> paramsMap) {
try {
//你项目方法的访问路径 例如 http://ip:端口/项目名/ceshi
String baseUrl = "http://****:18080/**";
String stringname = paramsMap.get("username");
String stringpwds = paramsMap.get("password");
String canshu= "?username="+stringname+"&password="+stringpwds;
String requestUrl = baseUrl+canshu;
System.err.println(requestUrl);
// 新建一个URL对象
URL url = new URL(requestUrl);
// 打开一个HttpURLConnection连接
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
// 设置连接超时时间
urlConn.setConnectTimeout(5 * 1000);
//设置从主机读取数据超时
urlConn.setReadTimeout(5 * 1000);
// Post请求必须设置允许输出 默认false
urlConn.setDoOutput(true);
//设置请求允许输入 默认是true
urlConn.setDoInput(true);
// Post请求不能使用缓存
urlConn.setUseCaches(false);
// 设置为Post请求
urlConn.setRequestMethod("POST");
//设置本次连接是否自动处理重定向
urlConn.setInstanceFollowRedirects(true);
//配置请求Content-Type
// urlConn.setRequestProperty("Content-Type", "application/json");//post请求不能设置这个
// 开始连接
urlConn.connect();
// 判断请求是否成功
if (urlConn.getResponseCode() == 200) {
// 获取返回的数据
String result = streamToString(urlConn.getInputStream());
if(result.equals("true")){
Intent intent = new Intent(MainActivity.this,LogoActivity.class);
startActivity(intent);
setContentView(R.layout.activity_logo);
}else{
Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_LONG).show();
}
} else {
Toast.makeText(this, "请求异常", Toast.LENGTH_LONG).show();
Log.e(TAG, "Post方式请求失败");
}
// 关闭连接
urlConn.disconnect();
} catch (Exception e) {
Log.e(TAG, e.toString());
}
}
这中调用不安全!
第二种调用方式
public void btnOk(View view) throws IOException, XmlPullParserException {
String names = nametex.getText().toString();
String pwds = passwordtex.getText().toString();
if (TextUtils.isEmpty(names) || TextUtils.isEmpty(pwds)) {
Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_LONG).show();
}else{
new Thread(postwsdl).start();
}
}
直接看下面的代码
//调用java 抛出的webservers
Runnable postwsdl= new Runnable() {
@Override
public void run() {
String names = nametex.getText().toString();
String pwds = passwordtex.getText().toString();
try {
LogoAccount(names,pwds);
} catch (Exception e) {
e.printStackTrace();
}
}
};
注意这种调用方式的前提是服务端抛出了web Server 服务 wsdl文件
//调用wsdl的接口
public void LogoAccount(String name,String pwd) throws Exception{
String WSDL_URI = "http://ip:port/webservices?wsdl";//wsdl 的uri
String namespace = "http://Impi.services.jksd.com/";//namespace
String methodName = "Account_isPasswordCorrect";//要调用的方法名称
SoapObject request = new SoapObject(namespace, methodName);
// 设置需调用WebService接口需要传入的两个参数mobileCode、userId
request.addProperty("arg0", name);//注意这里必须是arg0 否则 服务端收到的就是null
request.addProperty("arg1", pwd);
//创建SoapSerializationEnvelope 对象,同时指定soap版本号(之前在wsdl中看到的)
// xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 像这种没有说明版本的一般都是1.1的 也就是ver11
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapSerializationEnvelope.VER11);
envelope.bodyOut = request;//由于是发送请求,所以是设置bodyOut
envelope.dotNet = false;//由于是.net开发的webservice,所以这里要设置为true
HttpTransportSE httpTransportSE = new HttpTransportSE(WSDL_URI);
try {
httpTransportSE.call(null, envelope);//调用
// 获取返回的数据
SoapObject object = (SoapObject) envelope.bodyIn;
// 获取返回的结果
String result= object.getProperty(0).toString();
if(result.equals("true")){
Intent intent = new Intent(MainActivity.this,LogoActivity.class);
startActivity(intent);
setContentView(R.layout.activity_logo);
}else{
Toast.makeText(this, "用户名或密码错误", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
// TODO Auto-generated catch block
Toast.makeText(this, "请求异常", Toast.LENGTH_LONG).show();
}
}
代码可以直接使用,复制过去改改就可以了亲测有用!!!安卓新手不对 地方请大家指正!