package cn.edu.njupt.service;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import cn.edu.njupt.egou.Product_List;
import cn.edu.njupt.entity.EAppraise;
import cn.edu.njupt.entity.EProduct;
import cn.edu.njupt.entity.EUser;
import cn.edu.njupt.parser.AppraiseParser;
import cn.edu.njupt.parser.ProductParser;
import android.R.bool;
import android.R.string;
import android.util.Log;
public class Service {
/*
* @param username password
*/
public boolean login(String username,String password){
String re = null;
//调用web 服务
//1.使用SoapObject产生低矮用对象(参数:命名空间,方法名)
SoapObject callParam=new SoapObject(
"http://ws.njupt.edu.cn",
"Login");
//2.传递方法的参数
//erty("togoodsid",1);
callParam.addProperty("username",username);
callParam.addProperty("password",password);
//3.开始构造Soap的序列化封套(封装了wsdl的信息, 封装了其他相关输入输出的参数设置)
SoapSerializationEnvelope envelope=
new SoapSerializationEnvelope(SoapEnvelope.VER12);
//4.设置WS调用的参数
envelope.bodyOut=callParam;//设置调用的方法(含命名空间)
envelope.dotNet=true;//来自MS的.net写的WS也可以调用
envelope.setOutputSoapObject(callParam);//指定输出的对象
//5.构造WSDL的HTTP协议的请求对象http://127.0.0.1:8080/axis2/services/NP?wsdl
HttpTransportSE wsdl=new HttpTransportSE(
"http://192.168.1.102:8080/axis2/services/NUPTService?wsdl");
//6.请求调用
try{
wsdl.call("", envelope);
//7.返回调用结果
re=envelope.getResponse().toString();
Log.v("IIII",re);
}
catch(Exception e){
Log.v("IIII","Error:"+e.getMessage());
}
if(re.equals("true")){
return true;
}else {
return false;
}
/*
* ==比较的是地址
* equal比较的是内荣
*/
/*if(re == "ture"){
return true;
}else {
return false;
}*/
}
/*
* @param username,password
*/
public boolean register(string username,string password){
String re = null;
SoapObject callParam=new SoapObject("http://ws.njupt.edu.cn","register");
callParam.addProperty("username",username);
callParam.addProperty("password",password);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.bodyOut=callParam;//设置调用的方法(含命名空间)
envelope.dotNet=true;//来自MS的.net写的WS也可以调用
envelope.setOutputSoapObject(callParam);//指定输出的对象
HttpTransportSE wsdl=new HttpTransportSE(
"http://192.168.1.102:8080/axis2/services/NUPTService?wsdl");
try{
wsdl.call("", envelope);
re=envelope.getResponse().toString();
Log.v("IIII",re);
}
catch(Exception e){
Log.v("IIII","Error:"+e.getMessage());
}
if(re.equals("true")){
return true;
}else {
return false;
}
}
/*
* 重置密码
* @param username,npassword
*/
public boolean resetPwd(){
return false;
}
/*
* 搜索商品,文字搜索
* @param name
*/
public List<EProduct> searchEProducts(String name) throws JSONException{
String re = null;
SoapObject callParam=new SoapObject("http://ws.njupt.edu.cn","searchByName");
callParam.addProperty("name",name);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.bodyOut=callParam;//设置调用的方法(含命名空间)
envelope.dotNet=true;//来自MS的.net写的WS也可以调用
envelope.setOutputSoapObject(callParam);//指定输出的对象
HttpTransportSE wsdl=new HttpTransportSE(
"http://192.168.1.102:8080/axis2/services/NUPTService?wsdl");
try{
wsdl.call("", envelope);
re=envelope.getResponse().toString();
Log.v("IIII",re);
}
catch(Exception e){
Log.v("IIII","Error:"+e.getMessage());
}
//解析返回的数据
ProductParser productParser = new ProductParser();
List<EProduct> products = new ArrayList<EProduct>();
products = productParser.parseJSON(re);
return products;
}
//传送订单
/*
* 解析订单,why it is a json,not some others;
*/
public boolean sendOrder(){
//jsonOrder;
String jsonOrderString = null;
return false;
}
public List<EProduct> searchByName(String name){
String re = null;
SoapObject callParam=new SoapObject("http://ws.njupt.edu.cn","searchByName");
callParam.addProperty("name",name);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.bodyOut=callParam;//设置调用的方法(含命名空间)
envelope.dotNet=true;//来自MS的.net写的WS也可以调用
envelope.setOutputSoapObject(callParam);//指定输出的对象
HttpTransportSE wsdl=new HttpTransportSE(
"http://192.168.1.102:8080/axis2/services/NUPTService?wsdl");
try{
wsdl.call("", envelope);
re=envelope.getResponse().toString();
Log.v("IIII",re);
}
catch(Exception e){
Log.v("IIII","Error:"+e.getMessage());
}
//解析返回的数据
ProductParser productParser = new ProductParser();
List<EProduct> products = new ArrayList<EProduct>();
try {
products = productParser.parseJSON(re);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return products;
}
/*
* @param location
*
* 模糊查询,这样写是不行的
*/
public List<EProduct> searchByLocation(String location){
String re = null;
SoapObject callParam=new SoapObject("http://ws.njupt.edu.cn","searchByLocation");
callParam.addProperty("location",location);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.bodyOut=callParam;//设置调用的方法(含命名空间)
envelope.dotNet=true;//来自MS的.net写的WS也可以调用
envelope.setOutputSoapObject(callParam);//指定输出的对象
HttpTransportSE wsdl=new HttpTransportSE(
"http://192.168.1.102:8080/axis2/services/NUPTService?wsdl");
try{
wsdl.call("", envelope);
re=envelope.getResponse().toString();
Log.v("IIII",re);
}
catch(Exception e){
Log.v("IIII","Error:"+e.getMessage());
}
//解析返回的数据
ProductParser productParser = new ProductParser();
List<EProduct> products = new ArrayList<EProduct>();
try {
products = productParser.parseJSON(re);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return products;
}
public List<EProduct> searchByCode(String code){
String re = null;
SoapObject callParam=new SoapObject("http://ws.njupt.edu.cn","searchByCode");
callParam.addProperty("code",code);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.bodyOut=callParam;//设置调用的方法(含命名空间)
envelope.dotNet=true;//来自MS的.net写的WS也可以调用
envelope.setOutputSoapObject(callParam);//指定输出的对象
HttpTransportSE wsdl=new HttpTransportSE(
"http://192.168.1.102:8080/axis2/services/NUPTService?wsdl");
try{
wsdl.call("", envelope);
re=envelope.getResponse().toString();
Log.v("IIII",re);
}
catch(Exception e){
Log.v("IIII","Error:"+e.getMessage());
}
//解析返回的数据
ProductParser productParser = new ProductParser();
List<EProduct> products = new ArrayList<EProduct>();
try {
products = productParser.parseJSON(re);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return products;
}
/*
* @param name="productClassName"
*
*/
public List<EProduct> productListByClass(String productClassName){
String re = null;
SoapObject callParam=new SoapObject("http://ws.njupt.edu.cn","productListByClass");
callParam.addProperty("productClassName",productClassName);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.bodyOut=callParam;//设置调用的方法(含命名空间)
envelope.dotNet=true;//来自MS的.net写的WS也可以调用
envelope.setOutputSoapObject(callParam);//指定输出的对象
HttpTransportSE wsdl=new HttpTransportSE(
"http://192.168.1.102:8080/axis2/services/NUPTService?wsdl");
try{
wsdl.call("", envelope);
re=envelope.getResponse().toString();
Log.v("IIII",re);
}
catch(Exception e){
Log.v("IIII","Error:"+e.getMessage());
}
//解析返回的数据
ProductParser productParser = new ProductParser();
List<EProduct> products = new ArrayList<EProduct>();
try {
products = productParser.parseJSON(re);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return products;
}
/*
* @param username
* 返回值是:
*/
public EUser myZhanghao(String username){
return null;
}
/*
* @param name
*/
public boolean isNameUnique(String name){
String re = null;
SoapObject callParam=new SoapObject("http://ws.njupt.edu.cn","isNameUnique");
callParam.addProperty("name",name);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.bodyOut=callParam;//设置调用的方法(含命名空间)
envelope.dotNet=true;//来自MS的.net写的WS也可以调用
envelope.setOutputSoapObject(callParam);//指定输出的对象
HttpTransportSE wsdl=new HttpTransportSE(
"http://192.168.1.102:8080/axis2/services/NUPTService?wsdl");
try{
wsdl.call("", envelope);
re=envelope.getResponse().toString();
Log.v("IIII",re);
}
catch(Exception e){
Log.v("IIII","Error:"+e.getMessage());
}
//判断返回的结果
if(re.equals("true")){
return true;
}else{
return false;
}
}
/*
* @param username
*/
public int getScore(String username){
String re = null;
SoapObject callParam=new SoapObject("http://ws.njupt.edu.cn","getScore");
callParam.addProperty("username",username);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.bodyOut=callParam;//设置调用的方法(含命名空间)
envelope.dotNet=true;//来自MS的.net写的WS也可以调用
envelope.setOutputSoapObject(callParam);//指定输出的对象
HttpTransportSE wsdl=new HttpTransportSE(
"http://192.168.1.102:8080/axis2/services/NUPTService?wsdl");
try{
wsdl.call("", envelope);
re=envelope.getResponse().toString();
Log.v("IIII",re);
}
catch(Exception e){
Log.v("IIII","Error:"+e.getMessage());
}
//判断返回的结果
return Integer.parseInt(re);
}
/*
* @param productId
*/
public List<EAppraise> getProductAppraise(int productId){
String re = null;
SoapObject callParam=new SoapObject("http://ws.njupt.edu.cn","getProductAppraise");
callParam.addProperty("productId",productId);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.bodyOut=callParam;//设置调用的方法(含命名空间)
envelope.dotNet=true;//来自MS的.net写的WS也可以调用
envelope.setOutputSoapObject(callParam);//指定输出的对象
HttpTransportSE wsdl=new HttpTransportSE(
"http://192.168.1.102:8080/axis2/services/NUPTService?wsdl");
try{
wsdl.call("", envelope);
re=envelope.getResponse().toString();
Log.v("IIII",re);
}
catch(Exception e){
Log.v("IIII","Error:"+e.getMessage());
}
//判断
AppraiseParser appraiseParser = new AppraiseParser();
List<EAppraise> appraises = new ArrayList<EAppraise>();
try {
appraises = appraiseParser.parseJSON(re);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return appraises;
}
/*
* @param productId
*/
public EProduct getProduct(int productId){
String re = null;
SoapObject callParam=new SoapObject("http://ws.njupt.edu.cn","getProduct");
callParam.addProperty("productId",productId);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.bodyOut=callParam;//设置调用的方法(含命名空间)
envelope.dotNet=true;//来自MS的.net写的WS也可以调用
envelope.setOutputSoapObject(callParam);//指定输出的对象
HttpTransportSE wsdl=new HttpTransportSE(
"http://192.168.1.102:8080/axis2/services/NUPTService?wsdl");
try{
wsdl.call("", envelope);
re=envelope.getResponse().toString();
Log.v("IIII",re);
}
catch(Exception e){
Log.v("IIII","Error:"+e.getMessage());
}
//判断
ProductParser productParser = new ProductParser();
EProduct product = new EProduct();
try {
product = (EProduct) productParser.parseJSON(re);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return product;
}
/*
* @param content
* connection
*/
public void AddSuggest(String content,String connection){
String re = null;
SoapObject callParam=new SoapObject("http://ws.njupt.edu.cn","getProduct");
callParam.addProperty("content",content);
callParam.addProperty("connection",connection);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.bodyOut=callParam;//设置调用的方法(含命名空间)
envelope.dotNet=true;//来自MS的.net写的WS也可以调用
envelope.setOutputSoapObject(callParam);//指定输出的对象
HttpTransportSE wsdl=new HttpTransportSE(
"http://192.168.1.102:8080/axis2/services/NUPTService?wsdl");
try{
wsdl.call("", envelope);
re=envelope.getResponse().toString();
Log.v("IIII",re);
}
catch(Exception e){
Log.v("IIII","Error:"+e.getMessage());
}
}
/*
* @param username
* content
* productId
*/
public void addAppraisePic(String content,String connection,int productId){
String re = null;
SoapObject callParam=new SoapObject("http://ws.njupt.edu.cn","addAppraisePic");
callParam.addProperty("content",content);
callParam.addProperty("connection",connection);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.bodyOut=callParam;//设置调用的方法(含命名空间)
envelope.dotNet=true;//来自MS的.net写的WS也可以调用
envelope.setOutputSoapObject(callParam);//指定输出的对象
HttpTransportSE wsdl=new HttpTransportSE(
"http://192.168.1.102:8080/axis2/services/NUPTService?wsdl");
try{
wsdl.call("", envelope);
re=envelope.getResponse().toString();
Log.v("IIII",re);
}
catch(Exception e){
Log.v("IIII","Error:"+e.getMessage());
}
}
public void addAppraise(String content,String connection,int productId){
String re = null;
SoapObject callParam=new SoapObject("http://ws.njupt.edu.cn","addAppraise");
callParam.addProperty("content",content);
callParam.addProperty("connection",connection);
callParam.addAttribute("productId",productId);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.bodyOut=callParam;//设置调用的方法(含命名空间)
envelope.dotNet=true;//来自MS的.net写的WS也可以调用
envelope.setOutputSoapObject(callParam);//指定输出的对象
HttpTransportSE wsdl=new HttpTransportSE(
"http://192.168.1.102:8080/axis2/services/NUPTService?wsdl");
try{
wsdl.call("", envelope);
re=envelope.getResponse().toString();
Log.v("IIII",re);
}
catch(Exception e){
Log.v("IIII","Error:"+e.getMessage());
}
}
}