//所以......我这个webservice在数据库中有连接。我有两个webmethods:一个//用于数据库搜索,一个用于插入。我不知道我是否已连接到webservice //因为当我按任何按钮时我的应用程序退出。你能帮助我吗,非常感谢你!
package com.example.btn;
//import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
//import android.app.Activity;
//import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
//import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private static String SOAP_ACTION1 = "http://localhost/CheckRecords";
private static String SOAP_ACTION2 = "http://localhost/RecordData";
private static String NAMESPACE = "http://localhost/";
private static String METHOD_NAME1 = "CheckRecords";
private static String METHOD_NAME2 = "RecordData";
private static String URL = "http://http://192.168.100.176/WebS/Service1.asmx?wsdl";
// Button btnCheck,btnRecord;
// EditText barcodein,denumirein;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnCheck = (Button) findViewById(R.id.btnCheck);
Button btnRecord = (Button) findViewById(R.id.btnRecord);
final EditText barcodein = (EditText) findViewById(R.id.barcodein);
final EditText denumirein = (EditText) findViewById(R.id.denumirein);
btnCheck.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
//Use this to add parameters
request.addProperty("newbarcode",barcodein.getText().toString());
//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
//this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION1,envelope);
// Get the SoapResult from the envelope body.
SoapObject result = (SoapObject)envelope.bodyIn;
if(result != null)
{
//Get the first property and change the label text
denumirein.setText(result.getProperty(0).toString());
}
else
{
Toast.makeText(getApplicationContext(), "No data found",Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
btnRecord.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME2);
//Use this to add parameters
request.addProperty("newbarcode",barcodein.getText().toString());
//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);
envelope.dotNet = true;
try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
//this is the actual part that will call the webservice
androidHttpTransport.call(SOAP_ACTION2, envelope);
Toast.makeText(getApplicationContext(), "Inserted",Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
我有两个webmethods:一个用于数据库搜索,一个用于插入。 我不知道我是否已连接到Web服务,因为我的应用程序何时退出 我按任意按钮。
=>因为您尝试直接在主UI线程上进行网络调用。要解决此问题,您需要实施AsyncTask
。
我会要求您检查Logcat
输出,至少确切地给出错误/异常日志。