Android KSOAP2调试log:
1.
W/System.err( 3146): java.net.ConnectException: failed to connect to /192.168.1.150 (port 8080) after 20000ms: isConnected failed: ECONNREFUSED (Connection refused)
W/System.err( 3146): at libcore.io.IoBridge.isConnected(IoBridge.java:224)
原因: 服务器没有打开
2.
W/System.err( 3218): java.io.IOException: HTTP request failed, HTTP status: 500
W/System.err( 3218): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:195)
W/System.err( 3218): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:116)
W/System.err( 3218): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:111)
W/System.err( 3218): at com.example.complextyperpcclient.MainActivity$WSAsyncTask.doInBackground(MainActivity.java:136)
W/System.err( 3218): at com.example.complextyperpcclient.MainActivity$WSAsyncTask.doInBackground(MainActivity.java:1)
W/System.err( 3218): at android.os.AsyncTask$2.call(AsyncTask.java:287)
W/System.err( 3218): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
W/System.err( 3218): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
W/System.err( 3218): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
W/System.err( 3218): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
这个要查看tomcat/logs/目录下的catalina.out信息,发现找不到新增的 Decoder.BASE64Decoder jar包,所以我把jar包放入tomcat/webapps/axis2/WEB-INF/lib下,重启服务,即可OK
3.服务器端log:
1 [ERROR] 2
2 java.lang.ArrayIndexOutOfBoundsException: 2
3 at org.apache.axis2.databinding.utils.BeanUtil.deserialize(BeanUtil.java:630)
4 at org.apache.axis2.rpc.receivers.RPCUtil.processRequest(RPCUtil.java:153)
5 at org.apache.axis2.rpc.receivers.RPCUtil.invokeServiceClass(RPCUtil.java:206)
6 at org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:117)
7 at org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40)
8 at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:114)
9 at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:181)
10 at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:172)
11 at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:146)
12 at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
13 at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
14 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
15 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
16 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
17 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
18 at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
19 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
20 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
21 at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
22 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
23 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
24 at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
25 at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
26 at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
27 at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
28 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
29 at java.lang.Thread.run(Thread.java:619)
原因:
客户端在使用request.addProperty("filename", filename)时候,第一个参数名字一定要和服务器的方法参数名字一模一样,否则因为对不上号而出现序列化错误。
如下是从android客户端上传一张图片到电脑服务器的代码,已经调试OK.
服务器代码:
ComplexTypeService.java文件
import java.io.File;
import java.io.FileOutputStream;
import Decoder.BASE64Decoder;
import data.DataForm;
public class ComplexTypeService
{
// 上传图像,imageByte参数表示上传图像文件的字节,
// length参数表示图像文件的字节长度(该参数值可能小于imageByte的数组长度)
public String uploadImage(String imageString)
{
FileOutputStream fos = null;
try{
String toDir = "/home/lsc/"; //存储路径
byte[] buffer = new BASE64Decoder().decodeBuffer(imageString); //对android传过来的图片字符串进行解码
File destDir = new File(toDir);
if(!destDir.exists()) destDir.mkdir();
fos = new FileOutputStream(new File(destDir, "wahaha.png")); //保存图片
fos.write(buffer);
fos.flush();
fos.close();
System.out.print("sclu success\n");
System.out.print(imageString);
return "上传图片成功!" + "图片路径为:" + toDir;
}catch (Exception e){
e.printStackTrace();
}
return "上传图片失败!";
}
// 返回一维字符串数组
public String[] getArray()
{
String[] strArray = new String[]{ "自行车", "飞机", "火箭" };
return strArray;
}
// 返回二维字符串数组
public String[] getMDArray()
{
String[] strArray = new String[]{ "自行车,飞机,火箭","中国,美国,德国", "超人,蜘蛛侠,钢铁侠" } ;
return strArray;
}
// 返回DataForm类的对象实例
public DataForm getDataForm()
{
return new DataForm();
}
// 将DataForm类的对象实例序列化,并返回序列化后的字节数组
public byte[] getDataFormBytes() throws Exception
{
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(baos);
oos.writeObject(new DataForm());
return baos.toByteArray();
}
}
DataForm.java文件:
package data;
public class DataForm implements java.io.Serializable
{
private String name = "bill";
private int age = 20;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
}
把生成的ComplexTypeService.class包放入apache-tomcat-7.0.42/webapps/axis2/WEB-INF/pojo目录下;DataForm.class包放入apache-tomcat-7.0.42/webapps/axis2/WEB-INF/data下,然后启动tomcat服务即可。
MainActivity.java文件:
package com.example.complextyperpcclient;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.commons.codec.binary.Base64;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
private java.io.FileInputStream fis;
private SoapSerializationEnvelope envelope;
private HttpTransportSE ht;
private SoapObject request;
private static final String TAG = "SCLU complextyperpcclient";
private static final String TARGET_NAME_SPACE = "http://ws.apache.org/axis2";
private static final String WDSL_LINK = "http://192.168.1.150:8080/axis2/services/ComplexTypeService?wsdl";
private static final String CLASS_NAME = "ComplexTypeService";
private static final String GET_DATA = "getDataFormBytes";
private static final String GET_ARRAY ="getArray";
private static final String GET_MD_ARRAY ="getMDArray";
private static final String SET_IMAGE ="uploadImage";
private static final String GET_DATA_FORM ="getDataForm";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new WSAsyncTask().execute();
//callWebService();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class WSAsyncTask extends AsyncTask{
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
// 打开图像文件,确定图像文件的大小
java.io.File file = new java.io.File("/mnt/sdcard/Diagram2.png");
try {
fis = new java.io.FileInputStream("/mnt/sdcard/Diagram2.png");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 创建保存要上传的图像文件内容的字节数组
byte[] buffer = new byte[(int) file.length()];
// 将图像文件的内容读取buffer数组中
int n = 0;
try {
n = fis.read(buffer);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("文件长度:" + file.length());
Object[] opAddEntryArgs = new Object[]{ buffer, n};
Class[] classes = new Class[]{ Boolean.class };
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
baos.write(buffer);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String uploadbuffer = new String(Base64.encodeBase64(baos.toByteArray()));
//Soap Object 是 SoapSerializationEnvelope的重要组成部分
// SoapObject request = new SoapObject(TARGET_NAME_SPACE, GET_DATA_FORM);
// if(arg0[0] != null)
// request.addProperty("name", arg0[0]);
//
// SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
// envelope.bodyOut = request;
// envelope.dotNet = true;
//
// envelope.setOutputSoapObject(request);
// HttpTransportSE ht = new HttpTransportSE(WDSL_LINK);
// ht.debug = true;//是否开启调试
// try {
// ht.call(TARGET_NAME_SPACE + GET_DATA_FORM, envelope);
// SoapObject result=(SoapObject)envelope.getResponse();
// int count=result.getPropertyCount();
// String age = result.getProperty(0).toString();
// String name = result.getProperty(1).toString();
// Log.e(TAG, "count = " + count + " name = " + name + " age = " + age);
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (XmlPullParserException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
request = new SoapObject(TARGET_NAME_SPACE, SET_IMAGE);
// Log.e(TAG, "buff = " + uploadbuffer);
request.addProperty("imageString", uploadbuffer);
// request.addProperty("length", 100);
envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request;
envelope.dotNet = true;
envelope.encodingStyle="UTF-8";
envelope.setOutputSoapObject(request);
ht = new HttpTransportSE(WDSL_LINK);
try {
ht.call(TARGET_NAME_SPACE + SET_IMAGE, envelope);
SoapObject result=(SoapObject)envelope.bodyIn;
// int count=result.getPropertyCount();
Log.e(TAG, "count = " + result.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
//onPostExecute方法用于在执行完后台任务后更新UI,显示结果
@Override
protected void onPostExecute(String result) {
}
}
private void callWebService() {
Thread thr = new Thread() {
public void run() {
try {
String name = "sclu";
//Soap Object 是 SoapSerializationEnvelope的重要组成部分
SoapObject request = new SoapObject(TARGET_NAME_SPACE, SET_IMAGE);
request.addProperty("name", name);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request;
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE ht = new HttpTransportSE(WDSL_LINK);
ht.call("", envelope);
String ret = String.valueOf(envelope.getResponse());
Log.e("resultStr = ", ret);
} catch (SoapFault e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
};
thr.start();
}
}