-1

当我单击按钮时,我试图调用 Web 服务,按钮部分和输入工作现在我正在尝试实际调用 Web 服务,但我需要线程来这样做,但我不知道它们。人们说要使用 AsyncTask。

callWS 的 LogCat 日志: callWS:http://pastebin.com/nuLnPQz8

callWS FUNCTION:: 它总是会崩溃,因为它需要线程。

XML 代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/button_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Pošlji "
        android:onClick = "callWS"/>


    <EditText
        android:id="@+id/Text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="35dp"
        android:ems="10" />

    <TextView
        android:id="@+id/output"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/Text1"
        android:layout_marginTop="23dp"
        android:layout_toLeftOf="@+id/Text1"
        android:text="Output: "
        tools:context=".Activity123" />

</RelativeLayout>

Activity123.java 代码:

package com.test123;

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

import com.test123.R;

public class Activity123 extends Activity 
   {
      private static final String SOAP_ACTION = "http://192.168.0.25/webapplication1/ws.asmx";
      private static final String METHOD_NAME = "HelloWorld";
      private static final String NAMESPACE = "http://microsoft.com/webservices/";
      private static final String URL = "http://192.168.0.25/webapplication1/ws.asmx?WSDL";
      /** Called when the activity is first created. */

      @Override
      public void onCreate(Bundle savedInstanceState) 
         {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_activity123);
         }

      @Override
      public boolean onCreateOptionsMenu(Menu menu) 
         {
            getMenuInflater().inflate(R.menu.activity_activity123, menu);
            return true;
         }

  public void TEST(View button_one)
     {
        //int a = 2 + 3; // Runs fine
        //TextView text = (TextView) findViewById(R.id.output); // TextView text = (TextView) findViewById(R.id.button_one); // Runs fine
        //text.setText(String.valueOf(a)); IGNORE THIS ABOVE SECTION

        et = (EditText) findViewById(R.id.Text1); // et is our editText 
        String input = et.getText().toString(); // 

        TextView text = (TextView) findViewById(R.id.output); 
        text.setText( String.valueOf(input) ); 
        //setContentView(R.layout.secondary_activity);

        klicWS(input);
     }  

        public void klicWS(String input) // We receive input variable from TEST
           {
              //Thread networkThread = new Thread(); Threads like this... but this ain't correct....

              SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);                       
              SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
              envelope.setOutputSoapObject(request);
              envelope.dotNet = true;
              envelope.encodingStyle = SoapSerializationEnvelope.XSD;

              HttpTransportSE HT = new HttpTransportSE(URL);

              try 
                 {
                    HT.call(SOAP_ACTION, envelope);
                    SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
                    //Object result = (Object)envelope.getResponse(); // maybe important ?

                       {
                          TextView text = (TextView) findViewById(R.id.output);
                          text.setText( "Message from WS: "+response.toString()  );

                          //TextView tv = new TextView(this); this is from an example
                          //tv.setText( "Message :"+response.toString() ); // from example
                          setContentView(R.layout.secondary_activity); // The second one...
                       }
                 } 

             catch (Exception e) 
                {
                   e.printStackTrace();
                }
     } 

}

IDE(Eclipse)内的东西的屏幕截图,以及用于帮助的strings.xml: http ://www.filedropper.com/ideplusstringsxml 在此处输入图像描述

4

3 回答 3

1

这是一个常见的错误。您使用整数参数调用 TextView 的 setText 方法

int a = 2 + 3; // Runs fine
TextView text = (TextView) findViewById(R.id.button_one); 
text.setText(a); // makes ERROR

带有整数参数的 setText 方法旨在获取资源 ID (R.string.some_string_key)。

这就是为什么日志说

原因:android.content.res.Resources$NotFoundException:字符串资源 ID #0x5

您尝试从生成的 R.java 文件中获取 ID 为 5 的 strings.xml 字符串

只需将失败的行替换为

text.setText(String.valueOf(a)); // should work
于 2012-10-22T10:03:45.463 回答
0

单击按钮时,您正在尝试执行一些与网络相关的操作。在 Android 中,任何与网络相关的操作都不应该在主 UI 线程中完成。

因此,在按钮上单击启动一个新线程并从网络获取信息或使用异步任务。

谢谢和问候, Rajamanickem。小号

于 2012-10-22T09:57:14.287 回答
0

I think the problem is this line.

TextView text = (TextView) findViewById(R.id.button_one);

You convert your BUTTON button_one to a TEXTVIEW. I don't know if this is possible.

Try to use

TextView text = (TextView) findViewById(R.id.output);

and test if your code would work.

于 2012-10-22T09:53:05.163 回答