在android中使用soap webservices的mysql数据库连接

时间:2012-06-07 13:47:26

标签: java android mysql soap

我正在开发一个连接到Mysql数据库的Android应用程序,该数据库也使用SOAP Web服务

当我在Android模拟器上运行我的应用程序时,我看到以下消息。消息是  [Ljava.lang.String; @ 44f565fo。

我想了解我收到此消息的原因。

这是我的Java Web服务代码:

package com.retailer.ws;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
public class RetailerWS {
  public String customerData(){
  String customerInfo = "";
  try{
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/retailer","root","chathura");
    //Find customer information where the customer ID is maximum
    PreparedStatement statement
          =  con.prepareStatement("SELECT * FROM customers WHERE C_ID = (SELECT MAX(C_ID) FROM customer)");
    ResultSet result = statement.executeQuery();

    while(result.next()){
      customerInfo = customerInfo
                      + result.getString("name")
                      + "&" + result.getString("C_ID") 
                      + "&"+result.getString("address") 
                      + "&"+result.getString("email");
      // Here "&"s are added to the return string. 
      // This is help to split the string in Android application
    }
  } catch(Exception exc){
    System.out.println(exc.getMessage());
  }
    return customerInfo;
 }
}

以下是Android应用程序的代码

package com.retailer.client;

import android.app.Activity;
import android.os.Bundle;
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.widget.TextView;

public class RetailerActivity extends Activity {
  private static final String SOAP_ACTION = "http://ws.retailer.com/customerData";
  private static final String METHOD_NAME = "customerData";
  private static final String NAMESPACE = "http://ws.retailer.com/";
  private static final String URL = "http://175.157.234.156:8085/ForBlog/services/RetailerWS?wsdl";
  @Override
  public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

     envelope.setOutputSoapObject(request);

     HttpTransportSE ht = new HttpTransportSE(URL);
     try {
         ht.call(SOAP_ACTION, envelope);
         SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
         SoapPrimitive s = response;
         String str = s.toString();
         String resultArr[] = str.split("&");//Result string will split & store in an array

         TextView tv = new TextView(this);

         for(int i = 0; i<resultArr.length;i++){
           tv.append(resultArr[i]+"\n\n");
         }
         setContentView(tv);
     } catch (Exception e) {
        e.printStackTrace();
    }
   }
  }

main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

</LinearLayout>

Android清单文件也启用了Internet权限。

那我为什么要看[Ljava.lang.String; @ 44f565fo dis message?

这些是我的logcat窗口:

05-23 14:15:10.371: D/AndroidRuntime(3876): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
05-23 14:15:10.371: D/AndroidRuntime(3876): CheckJNI is ON
05-23 14:15:10.520: D/AndroidRuntime(3876): --- registering native functions ---
05-23 14:15:11.191: D/dalvikvm(179): GC_EXPLICIT freed 133 objects / 6720 bytes in 52ms
05-23 14:15:11.231: D/PackageParser(71): Scanning package: /data/app/vmdl70326.tmp
05-23 14:15:11.241: W/PackageParser(71): Unknown element under <application>: uses-permission at /data/app/vmdl70326.tmp Binary XML file line #21
05-23 14:15:11.341: I/PackageManager(71): Removing non-system package:com.retailer.client
05-23 14:15:11.341: I/ActivityManager(71): Force stopping package com.retailer.client uid=10040
05-23 14:15:11.361: I/Process(71): Sending signal. PID: 3868 SIG: 9
05-23 14:15:11.381: I/WindowManager(71): WIN DEATH: Window{44fbd530 com.retailer.client/com.retailer.client.RetailerActivity paused=false}
05-23 14:15:11.401: I/UsageStats(71): Unexpected resume of pack.GetImage while already resumed in com.retailer.client
05-23 14:15:11.421: W/InputManagerService(71): Got RemoteException sending setActive(false) notification to pid 3868 uid 10040
05-23 14:15:11.810: D/PackageManager(71): Scanning package com.retailer.client
05-23 14:15:11.810: I/PackageManager(71): Package com.retailer.client codePath changed from /data/app/com.retailer.client-2.apk to /data/app/com.retailer.client-1.apk; Retaining data and using new
05-23 14:15:11.810: I/PackageManager(71): /data/app/com.retailer.client-1.apk changed; unpacking
05-23 14:15:11.820: D/installd(35): DexInv: --- BEGIN '/data/app/com.retailer.client-1.apk' ---
05-23 14:15:12.000: D/dalvikvm(3883): DexOpt: 'Lorg/xmlpull/v1/XmlPullParser;' has an earlier definition; blocking out
05-23 14:15:12.000: D/dalvikvm(3883): DexOpt: 'Lorg/kxml2/io/KXmlParser;' has an earlier definition; blocking out
05-23 14:15:12.010: D/dalvikvm(3883): DexOpt: 'Lorg/xmlpull/v1/XmlSerializer;' has an earlier definition; blocking out
05-23 14:15:12.010: D/dalvikvm(3883): DexOpt: 'Lorg/kxml2/io/KXmlSerializer;' has an earlier definition; blocking out
05-23 14:15:12.010: D/dalvikvm(3883): DexOpt: 'Lorg/kxml2/kdom/Node;' has an earlier definition; blocking out
05-23 14:15:12.010: D/dalvikvm(3883): DexOpt: 'Lorg/kxml2/kdom/Document;' has an earlier definition; blocking out
05-23 14:15:12.010: D/dalvikvm(3883): DexOpt: 'Lorg/kxml2/kdom/Element;' has an earlier definition; blocking out
05-23 14:15:12.010: D/dalvikvm(3883): DexOpt: 'Lorg/xmlpull/v1/XmlPullParserException;' has an earlier definition; blocking out
05-23 14:15:12.010: D/dalvikvm(3883): DexOpt: 'Lorg/xmlpull/v1/XmlPullParserFactory;' has an earlier definition; blocking out
05-23 14:15:12.111: I/dalvikvm(3883): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
05-23 14:15:12.111: I/dalvikvm(3883): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
05-23 14:15:12.111: I/dalvikvm(3883): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
05-23 14:15:12.111: I/dalvikvm(3883): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
05-23 14:15:12.111: I/dalvikvm(3883): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
05-23 14:15:12.111: I/dalvikvm(3883): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
05-23 14:15:12.121: I/dalvikvm(3883): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
05-23 14:15:12.131: I/dalvikvm(3883): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParser;'
05-23 14:15:12.131: I/dalvikvm(3883): DexOpt: not resolving ambiguous class 'Lorg/kxml2/io/KXmlSerializer;'
05-23 14:15:12.141: D/dalvikvm(3883): DexOpt: not verifying 'Lorg/xmlpull/v1/XmlPullParser;': multiple definitions
05-23 14:15:12.141: D/dalvikvm(3883): DexOpt: not verifying 'Lorg/kxml2/io/KXmlParser;': multiple definitions
05-23 14:15:12.141: D/dalvikvm(3883): DexOpt: not verifying 'Lorg/xmlpull/v1/XmlSerializer;': multiple definitions
05-23 14:15:12.141: D/dalvikvm(3883): DexOpt: not verifying 'Lorg/kxml2/io/KXmlSerializer;': multiple definitions
05-23 14:15:12.141: D/dalvikvm(3883): DexOpt: not verifying 'Lorg/kxml2/kdom/Node;': multiple definitions
05-23 14:15:12.141: D/dalvikvm(3883): DexOpt: not verifying 'Lorg/kxml2/kdom/Document;': multiple definitions
05-23 14:15:12.141: D/dalvikvm(3883): DexOpt: not verifying 'Lorg/kxml2/kdom/Element;': multiple definitions
05-23 14:15:12.141: I/dalvikvm(3883): DexOpt: not resolving ambiguous class 'Lorg/xmlpull/v1/XmlPullParserException;'
05-23 14:15:12.161: D/dalvikvm(3883): DexOpt: not verifying 'Lorg/xmlpull/v1/XmlPullParserException;': multiple definitions
05-23 14:15:12.161: D/dalvikvm(3883): DexOpt: not verifying 'Lorg/xmlpull/v1/XmlPullParserFactory;': multiple definitions
05-23 14:15:12.171: D/dalvikvm(3883): DexOpt: load 53ms, verify 150ms, opt 7ms
05-23 14:15:12.181: D/installd(35): DexInv: --- END '/data/app/com.retailer.client-1.apk' (success) ---
05-23 14:15:12.191: W/PackageManager(71): Code path for pkg : com.retailer.client changing from /data/app/com.retailer.client-2.apk to /data/app/com.retailer.client-1.apk
05-23 14:15:12.191: W/PackageManager(71): Resource path for pkg : com.retailer.client changing from /data/app/com.retailer.client-2.apk to /data/app/com.retailer.client-1.apk
05-23 14:15:12.191: D/PackageManager(71):   Activities: com.retailer.client.RetailerActivity
05-23 14:15:12.201: I/ActivityManager(71): Force stopping package com.retailer.client uid=10040
05-23 14:15:12.331: I/installd(35): move /data/dalvik-cache/data@app@com.retailer.client-1.apk@classes.dex -> /data/dalvik-cache/data@app@com.retailer.client-1.apk@classes.dex
05-23 14:15:12.331: D/PackageManager(71): New package installed in /data/app/com.retailer.client-1.apk
05-23 14:15:12.490: I/ActivityManager(71): Force stopping package com.retailer.client uid=10040
05-23 14:15:12.630: D/dalvikvm(71): GC_EXPLICIT freed 7314 objects / 513320 bytes in 113ms
05-23 14:15:12.890: D/dalvikvm(71): GC_EXPLICIT freed 2977 objects / 155072 bytes in 101ms
05-23 14:15:12.890: D/dalvikvm(190): GC_EXPLICIT freed 878 objects / 50848 bytes in 197ms
05-23 14:15:12.950: W/RecognitionManagerService(71): no available voice recognition services found
05-23 14:15:13.081: I/installd(35): unlink /data/dalvik-cache/data@app@com.retailer.client-2.apk@classes.dex
05-23 14:15:13.100: D/AndroidRuntime(3876): Shutting down VM
05-23 14:15:13.100: D/dalvikvm(3876): Debugger has detached; object registry had 1 entries
05-23 14:15:13.131: I/dalvikvm(3876): JNI: AttachCurrentThread (from ???.???)
05-23 14:15:13.131: I/AndroidRuntime(3876): NOTE: attach of thread 'Binder Thread #3' failed
05-23 14:15:13.530: D/AndroidRuntime(3889): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
05-23 14:15:13.530: D/AndroidRuntime(3889): CheckJNI is ON
05-23 14:15:13.670: D/AndroidRuntime(3889): --- registering native functions ---
05-23 14:15:14.271: I/ActivityManager(71): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.retailer.client/.RetailerActivity }
05-23 14:15:14.301: I/ActivityManager(71): Start proc com.retailer.client for activity com.retailer.client/.RetailerActivity: pid=3895 uid=10040 gids={}
05-23 14:15:14.331: D/AndroidRuntime(3889): Shutting down VM
05-23 14:15:14.341: D/dalvikvm(3889): Debugger has detached; object registry had 1 entries
05-23 14:15:14.381: I/dalvikvm(3889): JNI: AttachCurrentThread (from ???.???)
05-23 14:15:14.381: I/AndroidRuntime(3889): NOTE: attach of thread 'Binder Thread #3' failed
05-23 14:15:14.500: D/dalvikvm(33): GC_EXPLICIT freed 259 objects / 10016 bytes in 189ms
05-23 14:15:14.630: D/dalvikvm(33): GC_EXPLICIT freed 17 objects / 712 bytes in 96ms
05-23 14:15:14.840: D/dalvikvm(33): GC_EXPLICIT freed 2 objects / 48 bytes in 214ms
05-23 14:15:15.020: W/System.err(3895): java.net.SocketException: Permission denied
05-23 14:15:15.020: W/System.err(3895):     at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocketImpl(Native Method)
05-23 14:15:15.030: W/System.err(3895):     at org.apache.harmony.luni.platform.OSNetworkSystem.createStreamSocket(OSNetworkSystem.java:186)
05-23 14:15:15.030: W/System.err(3895):     at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:265)
05-23 14:15:15.030: W/System.err(3895):     at java.net.Socket.checkClosedAndCreate(Socket.java:873)
05-23 14:15:15.030: W/System.err(3895):     at java.net.Socket.connect(Socket.java:1020)
05-23 14:15:15.030: W/System.err(3895):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:62)
05-23 14:15:15.030: W/System.err(3895):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:88)
05-23 14:15:15.030: W/System.err(3895):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHTTPConnection(HttpURLConnectionImpl.java:927)
05-23 14:15:15.030: W/System.err(3895):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:909)
05-23 14:15:15.040: W/System.err(3895):     at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:46)
05-23 14:15:15.040: W/System.err(3895):     at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:68)
05-23 14:15:15.040: W/System.err(3895):     at com.retailer.client.RetailerActivity.onCreate(RetailerActivity.java:31)
05-23 14:15:15.040: W/System.err(3895):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-23 14:15:15.040: W/System.err(3895):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-23 14:15:15.040: W/System.err(3895):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-23 14:15:15.040: W/System.err(3895):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-23 14:15:15.040: W/System.err(3895):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-23 14:15:15.040: W/System.err(3895):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 14:15:15.040: W/System.err(3895):     at android.os.Looper.loop(Looper.java:123)
05-23 14:15:15.040: W/System.err(3895):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-23 14:15:15.050: W/System.err(3895):     at java.lang.reflect.Method.invokeNative(Native Method)
05-23 14:15:15.050: W/System.err(3895):     at java.lang.reflect.Method.invoke(Method.java:521)
05-23 14:15:15.050: W/System.err(3895):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-23 14:15:15.050: W/System.err(3895):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-23 14:15:15.050: W/System.err(3895):     at dalvik.system.NativeStart.main(Native Method)
05-23 14:15:15.241: I/ActivityManager(71): Displayed activity com.retailer.client/.RetailerActivity: 945 ms (total 945 ms)
05-23 14:15:15.421: W/InputManagerService(71): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@44eb2660 (uid=10049 pid=3808)

2 个答案:

答案 0 :(得分:0)

错误在这里

W/System.err(3895): java.net.SocketException: Permission denied 

http://docs.oracle.com/javase/1.4.2/docs/api/java/net/SocketException.html

通读调用堆栈的跟踪

14:15:15.040: W/System.err(3895): 
at com.retailer.client.RetailerActivity.onCreate(RetailerActivity.java:31

表示执行

时存在权限问题
ht.call(SOAP_ACTION, envelope);

SocketException: Permission Denied?

使用其他Web服务测试工具(如SoapUi)查看您是否可以从运行模拟器的计算机建立与该Web服务的连接。

答案 1 :(得分:0)

您可能正在看到该值,因为您使用的方法是TextView.append()

如果查看this class的文档,您会看到字符串被转换为Charsequence.class,然后再次有一个字符数组......所以你要打印的只是值数组实例。它可能只是使用方法.toString()将数组转换为字符串:

setContentView(tv.toString());