1.可以改用SQL Server身份验证方式。在安全性-登录名中添加一个SQL Server身份验证方式登录的用户。
C#的代码里面数据库连接字符串还是粘贴属性里面的连接字符串,把密码改成自己的密码。
private String ConServerStr = "Data Source=2013-20160523DLInitial Catalog=testUser ID=houjingyiPassword=*******"
2.一定要先在webservice里面确认对数据库的 *** 作没有问题,再去调android程序。只看到页面出来了很可能数据库连接有问题,这样即使android程序没问题也调不出来。
3.android4.0以后不允许在主线程中访问网络,因为万一主线程阻塞了,会使得界面没有响应。我们开启线程访问即可。
[java] view plain copy
public List<HashMap<String, String>> getAllInfo(final Handler myhandler)
{
HashMap<String, String> tempHash = new HashMap<String, String>()
List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>()
tempHash.put("Cno", "Cno")
tempHash.put("Cname", "Cname")
tempHash.put("Cnum", "Cnum")
list.clear()
arrayList1.clear()
arrayList2.clear()
arrayList3.clear()
list.add(tempHash)
new Thread()
{
public void run()
{
arrayList1 = Soap.GetWebServer("selectAllCargoInfor", arrayList1, arrayList2)
Message msg=new Message()
msg.what=0x123
msg.obj=arrayList1
myhandler.sendMessage(msg)
}
}.start()
return list
}
public void insertCargoInfo(String Cname, String Cnum)
{
arrayList1.clear()
arrayList2.clear()
arrayList1.add("Cname")
arrayList1.add("Cnum")
arrayList2.add(Cname)
arrayList2.add(Cnum)
new Thread()
{
public void run()
{
try
{
Soap.GetWebServer("insertCargoInfo", arrayList1, arrayList2)
}
catch(Exception e)
{
}
}
}.start()
}
public void deleteCargoInfo(String Cno)
{
arrayList1.clear()
arrayList2.clear()
arrayList1.add("Cno")
arrayList2.add(Cno)
new Thread()
{
public void run()
{
try
{
Soap.GetWebServer("deleteCargoInfo", arrayList1, arrayList2)
}
catch(Exception e)
{
}
}
}.start()
}
4.android4.0以后子线程里是不能对主线程的UI进行改变的,因此就引出了Handler。主线程里定义Handler供子线程里使用。[java] view plain copy
final Handler myhandler=new Handler()
{
public void handleMessage(Message msg)
{
if(msg.what==0x123)
{
ArrayList<String> drrayList=(ArrayList<String>) msg.obj
for(int j=0!drrayList.isEmpty()&&j+2<drrayList.size()j+=3)
{
HashMap<String,String> hashMap=new HashMap<String,String>()
hashMap.put("Cno", drrayList.get(j))
hashMap.put("Cname", drrayList.get(j+1))
hashMap.put("Cnum", drrayList.get(j+2))
list.add(hashMap)
}
adapter=new SimpleAdapter(
MainActivity.this,list,
R.layout.adapter_item,
new String[]{"Cno","Cname","Cnum"},
new int[]{R.id.txt_Cno,R.id.txt_Cname,R.id.txt_Cnum})
listView.setAdapter(adapter)
}
}
}
Android客户端直接连接远程MySQL数据库的方法如下:String result = ""
//首先使用NameValuePair封装将要查询的年数和关键字绑定
ArrayList<NameValuePair>nameValuePairs = new ArrayList<NameValuePair>()
nameValuePairs.add(new BasicNameValuePair("year","1980"))
//使用HttpPost封装整个SQL语句
//使用HttpClient发送HttpPost对象
try{
HttpClient httpclient = new DefaultHttpClient()
HttpPost httppost = new HttpPost("http://example.com/getAllPeopleBornAfter.php")
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs))
HttpResponse response = httpclient.execute(httppost)
HttpEntity entity = response.getEntity()
InputStream is = entity.getContent()
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString())
}
//将HttpEntity转化为String
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8)
StringBuilder sb = new StringBuilder()
String line = null
while ((line = reader.readLine()) != null) {
sb.append(line + "\n")
}
is.close()
result=sb.toString()
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString())
}
//将String通过JSONArray解析成最终结果
try{
JSONArray jArray = new JSONArray(result)
for(int i=0i<jArray.length()i++){
JSONObject json_data = jArray.getJSONObject(i)
Log.i("log_tag","id: "+json_data.getInt("id")+
", name: "+json_data.getString("name")+
", sex: "+json_data.getInt("sex")+
", birthyear: "+json_data.getInt("birthyear")
)
}
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString())
}
虽然Android开发中可以直接连接数据库,但是实际中却不建议这么做,应该使用服务器端中转下完成。
1、首先输入代码
public void SendByHttpClient(final String id, final String pw){
new Thread(new Runnable() {
@Override
public void run() {
try {
HttpClient httpclient=new DefaultHttpClient()
HttpPost httpPost=new HttpPost("http://web应用部署服务器上的IP地址:/HttpClientDemo/Login")//服务器地址,指向Servlet
List<NameValuePair>params=new ArrayList<NameValuePair>()//将id和pw装入list
params.add(new BasicNameValuePair("ID",id))
params.add(new BasicNameValuePair("PW",pw))
final UrlEncodedFormEntity entity=new UrlEncodedFormEntity(params,"utf-8")//以UTF-8格式发送
httpPost.setEntity(entity)
HttpResponse httpResponse= httpclient.execute(httpPost)
if(httpResponse.getStatusLine().getStatusCode()==200)//在200毫秒之内接收到返回值
2、然后再输入下方的代码:
{
HttpEntity entity=httpResponse.getEntity()
String response=EntityUtils.toString(entity1, "utf-8")//以UTF-8格式解析
Message message=new Message()
message.what=USER_LOGIN
message.obj=response
handler.sendMessage(message)使用Message传递消息给线程
}
}
catch (Exception e) {
e.printStackTrace()
}
}
}).start()
}3、最终,测试结果图,如下:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)