ボタンをクリックすると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