-1

我正在制作一个 Android 应用程序,它现在应该连接到以前制作的 localhost Web 服务。事情进展不顺利。我有以下代码:

公共无效探针(){

    String soapMsg="<?xml version=\"1.0\" encoding=\"utf-8\"?>" + 
    "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
      "<soap:Body>"+
        "<SelectVezba xmlns=\"http://tempuri.org/\">"+
          "<ime>" + "a" + "</ime>" + 
        "</SelectVezba>" + 
      "</soap:Body>" +
    "</soap:Envelope>";


    Log.d("where", "before all others");

    TextView t = (TextView)findViewById(R.id.textView2);
        HttpParams httpParameters = new BasicHttpParams();
        // Set the timeout in milliseconds until a connection is established.
        int timeoutConnection = 15000;
        HttpConnectionParams.setConnectionTimeout(httpParameters,timeoutConnection);
        // Set the default socket timeout (SO_TIMEOUT)
        // in milliseconds which is the timeout for waiting for data.
        int timeoutSocket = 35000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

        DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);

        HttpPost httppost = new HttpPost(Constants.SERVICE_LOCATION);
        httppost.setHeader("Content-Type", Constants.CONTENT_TYPE);
        httppost.setHeader("SOAPAction", Constants.GET_SelectVezba);


        Log.d("where","before try");

        HttpEntity entity;
        try {
            entity = new StringEntity(soapMsg, HTTP.UTF_8);
            httppost.setEntity(entity);
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            Log.d("where", "in catch 1");
            e.printStackTrace();
        }


        HttpResponse response;
        try {
            Log.d("where", "in catch 0");
            response = httpclient.execute(httppost);
            Log.d("where","1");
            if(response == null)
            {

                Log.d("tag","response e null");
            }
            HttpEntity r_entity = response.getEntity(); // get response
            Header[] headers = response.getAllHeaders();
            if (r_entity != null) 
            {
              String str_entity = EntityUtils.toString(r_entity);
              Log.d("response",str_entity);
            }

        } catch (ClientProtocolException e) {
            Log.d("where", "in catch 2");
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.d("where", "in catch 3");
            e.printStackTrace();
        }// calling server

//parsing irrelevant to the question

          ParserSAXLista parser=new ParserSAXLista("nov.xml");
            ListView listview1 = (ListView)findViewById(R.id.listView1);
            ArrayList<Entitet> nova=parser.objekti();
            ArrayList<String> lista=new ArrayList<String>();

            for (Entitet en : nova) {
                lista.add(en.toString());
            }

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lista);
            listview1.setAdapter(adapter);

}

使用的常量定义如下:

    public final static String SERVICE_LOCATION = "http://10.0.2.2:63919/Servis.asmx";
    public final static String CONTENT_TYPE = "text/xml; charset=utf-8";
    public final static String GET_SelectVezba = "http://tempuri.org/SelectVezba";

在我的 LogCat 中,我得到一个“in catch 0”和一个“in catch 3”,它告诉我有一个异常来自该行:

response = httpclient.execute(httppost);

我该如何解决?我在其他地方读到我应该在清单中添加 Internet 权限,但是在写入时:

 <uses-permission android:name="android.permission.INTERNET"></uses-permission>

在清单中,程序立即开始崩溃。

任何帮助,将不胜感激。抱歉,问题中有这么长的代码。

  • 编辑

日志猫:

07-11 09:10:54.224: D/AndroidRuntime(1355): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
07-11 09:10:54.224: D/AndroidRuntime(1355): CheckJNI is ON
07-11 09:10:54.344: D/dalvikvm(1355): Trying to load lib libjavacore.so 0x0
07-11 09:10:54.354: D/dalvikvm(1355): Added shared lib libjavacore.so 0x0
07-11 09:10:54.394: D/dalvikvm(1355): Trying to load lib libnativehelper.so 0x0
07-11 09:10:54.394: D/dalvikvm(1355): Added shared lib libnativehelper.so 0x0
07-11 09:10:55.244: D/AndroidRuntime(1355): Calling main entry com.android.commands.pm.Pm
07-11 09:10:55.285: D/AndroidRuntime(1355): Shutting down VM
07-11 09:10:55.304: D/dalvikvm(1355): GC_CONCURRENT freed 100K, 19% free 470K/576K, paused 1ms+5ms, total 16ms
07-11 09:10:55.304: D/dalvikvm(1355): Debugger has detached; object registry had 1 entries
07-11 09:10:56.984: D/AndroidRuntime(1368): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
07-11 09:10:56.984: D/AndroidRuntime(1368): CheckJNI is ON
07-11 09:10:57.329: D/dalvikvm(1368): Trying to load lib libjavacore.so 0x0
07-11 09:10:57.354: D/dalvikvm(1368): Added shared lib libjavacore.so 0x0
07-11 09:10:57.474: D/dalvikvm(1368): Trying to load lib libnativehelper.so 0x0
07-11 09:10:57.474: D/dalvikvm(1368): Added shared lib libnativehelper.so 0x0
07-11 09:10:58.914: D/AndroidRuntime(1368): Calling main entry  com.android.commands.am.Am
07-11 09:10:58.924: D/dalvikvm(1368): Note: class Landroid/app/ActivityManagerNative; has 156 unimplemented (abstract) methods
07-11 09:10:58.964: I/ActivityManager(269): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.anotherstart/.MainActivity} from pid 1368
07-11 09:10:58.994: W/WindowManager(269): Failure taking screenshot for (328x583) to  layer 21010
07-11 09:10:59.054: D/AndroidRuntime(1368): Shutting down VM
07-11 09:10:59.084: D/dalvikvm(1368): GC_CONCURRENT freed 101K, 17% free 501K/604K, paused 1ms+1ms, total 29ms
07-11 09:10:59.104: D/jdwp(1368): Got wake-up signal, bailing out of select
07-11 09:10:59.104: D/dalvikvm(1368): Debugger has detached; object registry had 1 entries
07-11 09:10:59.234: D/dalvikvm(1380): Not late-enabling CheckJNI (already on)
07-11 09:10:59.244: I/ActivityManager(269): Start proc com.example.anotherstart for activity com.example.anotherstart/.MainActivity: pid=1380 uid=10048 gids={50048, 3003, 1028}
07-11 09:10:59.644: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
07-11 09:10:59.994: E/Trace(1380): error opening trace file: No such file or directory (2)
07-11 09:11:02.296: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
07-11 09:11:02.684: D/gralloc_goldfish(1380): Emulator without GPU emulation detected.
07-11 09:11:03.244: I/ActivityManager(269): Displayed com.example.anotherstart/.MainActivity: +4s57ms (total +13m39s598ms)
07-11 09:11:11.134: I/ActivityManager(269): START u0 {cmp=com.example.anotherstart/.AddHrana} from pid 1380
07-11 09:11:11.134: W/WindowManager(269): Failure taking screenshot for (328x583) to layer 21015
07-11 09:11:11.654: D/kade(1380): pred site drugi
07-11 09:11:11.654: D/kaj sum(1380): pred try
07-11 09:11:11.674: D/kade(1380): vo catch 0
07-11 09:11:11.844: D/AndroidRuntime(1380): Shutting down VM
07-11 09:11:11.864: W/dalvikvm(1380): threadid=1: thread exiting with uncaught exception (group=0x2bd39930)
07-11 09:11:11.934: E/AndroidRuntime(1380): FATAL EXCEPTION: main
07-11 09:11:11.934: E/AndroidRuntime(1380): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.anotherstart/com.example.anotherstart.AddHrana}: android.os.NetworkOnMainThreadException
07-11 09:11:11.934: E/AndroidRuntime(1380):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at android.os.Looper.loop(Looper.java:137)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at android.app.ActivityThread.main(ActivityThread.java:5039)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at java.lang.reflect.Method.invokeNative(Native Method)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at java.lang.reflect.Method.invoke(Method.java:511)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at dalvik.system.NativeStart.main(Native Method)
07-11 09:11:11.934: E/AndroidRuntime(1380): Caused by: android.os.NetworkOnMainThreadException
07-11 09:11:11.934: E/AndroidRuntime(1380):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at libcore.io.IoBridge.connectErrno(IoBridge.java:144)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at libcore.io.IoBridge.connect(IoBridge.java:112)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at java.net.Socket.connect(Socket.java:842)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at com.example.anotherstart.AddHrana.proben(AddHrana.java:92)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at  com.example.anotherstart.AddHrana.onCreate(AddHrana.java:144)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at android.app.Activity.performCreate(Activity.java:5104)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-11 09:11:11.934: E/AndroidRuntime(1380):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-11 09:11:11.934: E/AndroidRuntime(1380):     ... 11 more
07-11 09:11:12.034: D/dalvikvm(1380): GC_CONCURRENT freed 198K, 3% free 9137K/9380K, paused 75ms+16ms, total 321ms
07-11 09:11:12.034: D/dalvikvm(1380): WAIT_FOR_CONCURRENT_GC blocked 60ms
07-11 09:11:12.064: W/ActivityManager(269):   Force finishing activity com.example.anotherstart/.AddHrana
07-11 09:11:12.084: W/ActivityManager(269):   Force finishing activity com.example.anotherstart/.MainActivity
07-11 09:11:12.494: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
07-11 09:11:12.594: W/ActivityManager(269): Activity pause timeout for ActivityRecord{2cae4aa0 u0 com.example.anotherstart/.AddHrana}
07-11 09:11:13.184: I/Choreographer(269): Skipped 45 frames!  The application may be doing too much work on its main thread.
07-11 09:11:13.274: I/MIPSAssembler(36): generated scanline__00000077:03515104_00008001_00000000 [206 ipp] (245 ins) at [0x2d401970:0x2d401d44] in 0 ns
07-11 09:11:13.484: I/Choreographer(1145): Skipped 63 frames!  The application may be doing too much work on its main thread.
07-11 09:11:13.794: E/SurfaceFlinger(36): ro.sf.lcd_density must be defined as a build property
07-11 09:11:14.431: I/Choreographer(1145): Skipped 97 frames!  The application may be doing too much work on its main thread.
07-11 09:11:26.971: W/ActivityManager(269): Activity destroy timeout for ActivityRecord{2cb65278 u0 com.example.anotherstart/.MainActivity}
07-11 09:11:26.971: W/ActivityManager(269): Activity destroy timeout for ActivityRecord{2cae4aa0 u0 com.example.anotherstart/.AddHrana}

显现:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.anotherstart.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.anotherstart.Pregled"
        android:label="@string/title_activity_pregled" >
    </activity>
    <activity
        android:name="com.example.anotherstart.AddHrana"
        android:label="@string/title_activity_add_hrana" >
    </activity>
    <activity
        android:name="com.example.anotherstart.AddVezba"
        android:label="@string/title_activity_add_vezba" >
    </activity>
</application>

4

2 回答 2

1

查看您的 logcat 输出,我们可以看到:

(...)
07-11 09:11:11.934: E/AndroidRuntime(1380): Caused by: android.os.NetworkOnMainThreadException
(...)

在某条线上。

这符合@Selvin 关于“ android.os.NetworkOnMainThreadException ”可能重复的评论。

正如那个答案所说:AsyncTask 解决方案

您应该在 AsyncTask 对象中完成所有网络工作,然后传递最终结果(或 progressResult),并且由于 AsyncTask 对象,将在 UI 线程上调用(您将在其中处理您的 UI 元素/事件/答案)。

不要犹豫,看看:AsyncTask 文档

于 2013-07-11T09:26:24.057 回答
0

请参阅如何使用 SOAP 在 Android 中调用 Web 服务教程以使用 SOAP 连接 Web 服务

于 2013-07-11T09:23:51.600 回答