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

Posted

技术标签:

【中文标题】在android中使用soap webservices的mysql数据库连接【英文标题】:mysql database connection using soap webservices in android 【发布时间】:2012-06-11 14:05:39 【问题描述】:

我正在开发一个连接到 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_
android:layout_
android:orientation="vertical" >

<TextView
    android:layout_
    android:layout_
    android:text="@string/hello" />

</LinearLayout>

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

那么为什么我会看到 [Ljava.lang.String;@44f565fo 显示消息?

这是我的 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: Window44fbd530 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)

【问题讨论】:

嗨,harikrish,查看如何在启动应用程序时检查 Android 日志文件并监控它们。您在此处看到的任何错误消息都应该可以帮助您解决此问题。如果您没有看到与数据库或 Web 服务连接相关的错误消息,那么您可以将日志消息添加到您的 Android Applciation 代码中,以查看您的应用程序的进展情况。 developer.android.com/resources/faq/commontasks.html#logging 此外,您应该暂时注释掉您的 SOAP 代码,以便您看到我假设您在 strings.xml 中定义的@string/hello? 是时候学习提出具体问题了;也许gist.github.com 的用处请查看 www.***.com/faq 否则这个问题有被 *** 版主关闭的风险。 【参考方案1】:

错误在这里

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 服务的连接。

【讨论】:

【参考方案2】:

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

如果您查看this class 的文档,您会看到该字符串已转换为 Charsequence.class,然后您又拥有一个字符数组...所以您打印的只是数组的值实例。只需使用 .toString() 方法将数组转换为字符串即可:

setContentView(tv.toString());

【讨论】:

以上是关于在android中使用soap webservices的mysql数据库连接的主要内容,如果未能解决你的问题,请参考以下文章

VC++中使用SOAP发生错误~

Java实现SOAP协议 之 HelloWorld

Java发布webservice应用并发送SOAP请求调用

如何在 Java Android App 中使用从 Webserver 检索到的 JSON 来动态创建和使用对象

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

使用soap webservice在android自己的应用程序中推送通知