Android+webService的连接

</pre><pre name="code" class="java"><div>  <span style="font-size:24px;"> 最近初步在搞android怎么去跟webservice的连接,调用参数返回。在这里发下我运行的源码。已测试,已成功! </span></div><div>
</div>
<span style="font-size:18px;">import android.widget.EditText;
public class LQserviceActivity extends Activity {
	/** Called when the activity is first created. */
	private Button button;
	private EditText editText;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		button = (Button) findViewById(R.id.button);
		editText = (EditText) findViewById(R.id.edit);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String show = editText.getText().toString();
				String showMessage = "";
				try {
					showMessage = getPhoneInfo(show);
					editText.setText(showMessage);
				} catch (XmlPullParserException e) {
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		});
	}
	public String getPhoneInfo(String phoneName) throws IOException,
			XmlPullParserException {
		// 获取URL
		String URL = "http://61.143.165.38/wlss.asmx";
		// 返回的查询结果
		String result = null;
		// 调用webservice接口的命名空间
		String namespace = "http://61.143.165.38/";
		// 调用的方法名
		String methodName = "HelloWorld";
		// webservice中SOAPAction的地址
		String SOAP_ACTION = "http://www.1008656.com/HelloWorld";
		// 获得返回请求对象
		SoapObject request = new SoapObject(namespace, methodName);
		// 设置需要返回请求对象方法的参数
		// request.addProperty("moblieCode",phoneName);
		// request.addProperty("userId","");
		// 设置soap的版本
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		// 设置是否调用的是dotNet开发的,这里如果设置为TRUE,那么在服务器端将获取不到参数值(如:将这些数据插入到数据库中的话)
		envelope.dotNet = true;
		envelope.bodyOut = request;
		AndroidHttpTransport hts = new AndroidHttpTransport(URL);
		// web service请求
		hts.call(SOAP_ACTION, envelope);
		// hts.call(null,envelope);
		// 得到返回结果
		Object o = envelope.getResponse();
		result = o.toString();
		return result;
	}
}</span>
<span style="font-size:18px;">
<strong>main.xml的源码如下:</strong></span>
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <!-- android:text="webservice的调用" -->
    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="点击" />
    <EditText
        android:id="@+id/edit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
</span>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要通过Android连接SQL Server并使用WebService,您可以按照以下步骤进行操作: 1. 首先,确保您已经创建了一个用于SQL Server数据库的服务器以及相应的表。 2. 下载并安装适用于Android开发的IDE(如Android Studio)并创建新的Android项目。 3. 在Android项目的build.gradle文件中,添加Java连接SQL Server所需的依赖项。示例代码如下: ```java dependencies { implementation 'net.sourceforge.jtds:jtds:1.3.1' } ``` 4. 在Android的MainActivity.java文件中,编写以下代码来创建使用WebService连接SQL Server的逻辑: ```java import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private static final String ip = "your_server_ip_address"; private static final String port = "your_sql_server_port"; private static final String database = "your_database_name"; private static final String user = "your_username"; private static final String password = "your_password"; private Button connectButton; private TextView resultTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); connectButton = findViewById(R.id.connectButton); resultTextView = findViewById(R.id.resultTextView); connectButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new ConnectSQLServer().execute(); } }); } private class ConnectSQLServer extends AsyncTask<Void, Void, String> { @Override protected String doInBackground(Void... voids) { String result = ""; try { Class.forName("net.sourceforge.jtds.jdbc.Driver"); String url = "jdbc:jtds:sqlserver://" + ip + ":" + port + "/" + database; Connection connection = DriverManager.getConnection(url, user, password); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("your_sql_query_here"); while (resultSet.next()) { result += resultSet.getString("your_column_name"); } resultSet.close(); statement.close(); connection.close(); } catch (Exception e) { Log.e(TAG, "Error: " + e.getMessage()); result = "Error: " + e.getMessage(); } return result; } @Override protected void onPostExecute(String result) { resultTextView.setText(result); } } } ``` 通过上述代码,当用户点击Connect按钮时,将会创建一个异步任务来连接SQL Server并执行查询。请确保将您的服务器IP地址、端口、数据库名称、用户名和密码替换为实际值。 5. 在Android的布局文件(activity_main.xml)中,创建一个Button和一个TextView用于显示查询结果。 ```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" android:paddingLeft="16dp" android:paddingTop="16dp" android:paddingRight="16dp" android:paddingBottom="16dp"> <Button android:id="@+id/connectButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Connect" android:layout_centerInParent="true"/> <TextView android:id="@+id/resultTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/connectButton" android:textSize="18sp"/> </RelativeLayout> ``` 6. 运行Android应用程序,并点击Connect按钮以连接SQL Server并显示查询结果。 这就是使用Android连接SQL Server并使用WebService的详细教程。请注意,这只是一个基本示例,您可能需要根据您的特定需求进行一些调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值