Java 我需要帮助添加线程到web服务的连接,否则它将无法运行
当我点击按钮时,我试图调用一个web服务,按钮部分和输入现在起作用了。我试图实际调用web服务,但我需要线程,但我不知道它们。人们说要使用异步任务 callWS的LogCat日志: callWS:“” callWS函数:: 它总是会崩溃,因为它需要线程 XML代码:Java 我需要帮助添加线程到web服务的连接,否则它将无法运行,java,android,Java,Android,当我点击按钮时,我试图调用一个web服务,按钮部分和输入现在起作用了。我试图实际调用web服务,但我需要线程,但我不知道它们。人们说要使用异步任务 callWS的LogCat日志: callWS:“” callWS函数:: 它总是会崩溃,因为它需要线程 XML代码: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.androi
<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>
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:
我想问题出在这条线路上
TextView text = (TextView) findViewById(R.id.button_one);
您可以将按钮转换为文本视图。我不知道这是否可能
尝试使用
TextView text = (TextView) findViewById(R.id.output);
并测试您的代码是否工作。单击按钮时,您正在尝试执行一些与网络相关的操作。在Android中,任何与网络相关的操作都不应该在主UI线程中完成 因此,在按钮上单击“启动新线程并从网络获取信息”或“使用异步任务” 谢谢和问候,
拉贾曼尼克姆。这是一个常见的错误。使用整数参数调用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\u string\u key)
这就是为什么日志说
原因:android.content.res.Resources$NotFoundException:String
资源ID#0x5
您尝试从生成的R.java文件中获取一个ID为5的strings.xml字符串
只需将出现故障的线路更换为
text.setText(String.valueOf(a)); // should work
请你把这些文件放在像pastebin这样的地方好吗?如果不在4shared注册,我实际上无法查看它们来帮助您。这里有一个链接给你:谢谢!更新了日志,但对于截图,我忘记了我使用的网站的名称,以前它不需要任何东西,但现在它需要一个简单的登录,这是伟大的。。。这是一件+分享+分享的事情。。。有人知道一个好的图像共享网站吗?我试过imgur.com,但在firefox和explorer上它只停留在13%和0%…完成了,我想我必须再问一个问题。。。第一个小时所有答案,然后第二个小时什么都没有…不一样。。。(有人不喜欢我,这就是Button扩展TextView的原因,所以演员阵容应该很好,现在我只有callWH+Celsium到Fe…剩下的事情要做了。看起来setText不喜欢int值。你可以像这样解决这个问题。setText(a+“”);快速简单;这是真的,但不相关。我可以在某个地方获得一些链接或教程吗…因为我没有android java的大脑…我只是想让这个应用程序完成,永远不会回来。我可以知道如何做吗?没问题。这就是你的应用程序崩溃的原因。我因为这些问题撞了墙3天…我将继续这样做,直到这个应用程序已经完成,可以连接到任何WS,天哪,我只有17岁,他让我这么做了…(他获得了博士学位…)