Asked the teacher to be a small project, you need to connect to the database using a mobile phone, online search webservice my best to use C # to write a webservice
DBOperation.cs:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
namespace SecurityService
{
public class DBOperation: IDisposable
{
public static SqlConnection sqlCon; / / used to connect to the database
/ / The contents of the following quotes replaced between the properties recorded in the above connection string
private String ConServerStr = @ "Data Source = ARRON-PC; Initial Catalog = Security; Integrated Security = True";
/ / Default constructor
public DBOperation ()
{
if (sqlCon == null)
{
sqlCon = new SqlConnection ();
sqlCon.ConnectionString = ConServerStr;
sqlCon.Open ();
}
}
/ / Close / destroy function, equivalent to Close ()
public void Dispose ()
{
if (sqlCon! = null)
{
sqlCon.Close ();
sqlCon = null;
}
}
/ / /
/ / / Get information
all goods/ / /
/ / /All cargo information
public ListShowInfo ()
{
Listlist = new List ();
try
{
string sql = "select * from Find";
SqlCommand cmd = new SqlCommand (sql, sqlCon);
SqlDataReader reader = cmd.ExecuteReader ();
while (reader.Read ())
{
/ / Add the information to the result set returned vector
list.Add (reader [0] ToString ().);
list.Add (reader [1] ToString ().);
list.Add (reader [2] ToString ().);
}
reader.Close ();
cmd.Dispose ();
}
catch (Exception)
{
}
return list;
}
}
}
Service1.samx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace SecurityService
{
/ / /
/ / / Service1 Summary Description
/ / /
[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem (false)]
/ / To allow the use of ASP.NET AJAX call this Web service from a script, please cancel the downlink comments.
/ / [System.Web.Script.Services.ScriptService]
public class Service1: System.Web.Services.WebService
{
DBOperation dbOperation = new DBOperation ();
[WebMethod]
public string [] ShowInfo ()
{
return dbOperation.ShowInfo () ToArray ();.
}
}
}
Here is the Android side code:
MainActivity.java:
package com.example.demo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
Button btn;
ListView lst;
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
btn = (Button) findViewById (R.id.btn1);
lst = (ListView) findViewById (R.id.listview1);
btn.setOnClickListener (new OnClickListener () {
@ Override
public void onClick (View v) {
/ / TODO Auto-generated method stub
lst.setAdapter (new SimpleAdapter (MainActivity.this, getData (),
R.layout.adapter_item, new String [] {"Location", "Description", "Phone"},
new int [] {R.id.txt_Location, R.id.txt_Description, R.id.txt_Phone}));
}
});
}
public List> getData () {
DBUtil dbUtil = new DBUtil ();
List> list = dbUtil.getAllInfo ();
return list;
}
@ 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;
}
}
DBUtil.java:
package com.example.demo;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.util.Log;
public class DBUtil {
private ArrayListcrrayList = new ArrayList ();
private SoapUlti Soap = new SoapUlti ();
/ **
* Get information
all goods*
* @ Return
* /
public List> getAllInfo () {
List> list = new ArrayList > ();
crrayList.clear ();
crrayList = Soap.GetWebServre ("ShowInfo");
HashMaptempHash = new HashMap ();
tempHash.put ("Location", "");
tempHash.put ("Description", "");
tempHash.put ("Phone", "");
list.add (tempHash);
for (int j = 0; jHashMap hashMap = new HashMap ();
hashMap.put ("Cno", crrayList.get (j));
hashMap.put ("Cname", crrayList.get (j + 1));
hashMap.put ("Cnum", crrayList.get (j + 2));
list.add (hashMap);
}
return list;
}
}
then SoapUlti.java class can not write up a little button to run directly off the application, seeking help to see how big God I wrote this for my class ah Soap , to really be able to run a project to look at the line ah really is not a solution tangle many days

<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
http request into sub-thread.
Reply:
I'm not good at C #, but still posted the code, lz see if there are useful:
Service side:
package com.login.webservice;
import java.util.List;
import org.apache.commons.mvc.annotation.Webservice;
import org.apache.commons.mvc.annotation.WebserviceMethod;
import org.apache.commons.mvc.entity.Json;
import org.apache.commons.mvc.util.ToolUtil;
import org.apache.log4j.Logger;
import com.login.entity.CarMember;
import com.login.model.CarMemberModel;
/ **
* User Interface
*
* @ Author TCK-001
* @ Version 1.0
* /
@ Webservice (name = "carMemberWs")
public class CarMemberWs {
private static final Logger log = Logger.getLogger (CarMemberWs.class);
/ **
* User login
* @ Param name
* @ Param pwd
* @ Return
* /
@ WebserviceMethod
public Json login (String name, String pwd) {
try {
CarMember member = new CarMemberModel () getByName (name);.
if (null == member) {
return new Json (false, "No user");
}
if (! ToolUtil.md5 (ToolUtil.deUnicode (pwd)). equals (member.getPassword ())) {
return new Json (false, "Wrong password");
}
. return new Json (true, Json.OPERATE_RIGHT_MESSAGE) addData ("member", member);
} Catch (Exception e) {
log.error ("User Login", e);
return new Json (false, e.getMessage ());
}
}
/ **
* All users
* @ Return
* /
@ WebserviceMethod
public Listlist () {
return new CarMemberModel () getBeans ();.
}
}
android side:
/ **
* Methods login
* @ Param view
* /
public void doLogin (View view) {
if (toolUtil.isBlank (txtName.getText (). toString ())) {
prompt (getString (R.string.userNameErr));
return;
}
if (toolUtil.isBlank (txtPwd.getText (). toString ())) {
prompt (getString (R.string.passwordErr));
return;
}
gress = ProgressDialog.show (this, getString (R.string.wait), getString (R.string.loadData), true);
gress.setCanceledOnTouchOutside (true);
new LoginThread () start ();.
}
/ / Prompts
private void prompt (String msg) {
Toast.makeText (this, msg, Toast.LENGTH_LONG) show ();.
}
/ / Login thread
private class LoginThread extends Thread {
public void run () {
try {
/ / Prepare parameters
Object [] param = new Object [] {txtName.getText () toString (), toolUtil.toUnicode (txtPwd.getText () toString ().).};
/ / Prepare to call Webservice client
WebserviceClient client = new WebserviceClient (wsUrl, "carMemberWs", "login", param, String.class, null);
/ / Call the Webservice, and to return to decompress and then decrypt data
response = toolUtil.gunzip ((String) client.execute ());
} Catch (Exception e) {
Log.e (Constant.TAG_NAME, "login thread", e);
} Finally {
loginHandler.sendMessage (new Message ());
}
}
}
/ / Login Handler
private Handler loginHandler = new Handler () {
public void handleMessage (Message msg) {
try {
gress.dismiss ();
if (! toolUtil.isBlank (response)) {
JSONObject json = new JSONObject (response);
if (json.getBoolean ("success")) {
/ / Get the user object
jsonCarMember member = beanUtil.jsonToBean (json.getJSONObject ("data") getJSONObject ("member"), CarMember.class.);
member.setPassword (txtPwd.getText () toString ().);
/ / Save the account password serialized to a file
fileUtil.serialize (rootPath + filePath, member);
response = null;
prompt ("OK");
} Else {
prompt (json.getString ("message"));
}
} Else {
prompt (getString (R.string.fail));
}
} Catch (Exception e) {
prompt (e.getMessage ());
}
}
};
Reply:
Network-related operations to put the child thread, can not operate in the main UI thread. Another exception log can be posted out better analysis.
Reply:
Seems to be the problem, I do not know how to put solving
Reply:
Solving how to put
Reply:
http://www.2cto.com/kf/201402/281526.html
Reply:
thank useful
Reply:
On new a Thread, then run inside rewritten interface, network operations inside, after the completion callback interfaces or static data storage results.
If you are not familiar with the child thread to the UI from communication, can Baidu under AsyncTask usage, which has performed in the UI thread callback interface, convenient point.
No comments:
Post a Comment