安卓连接云数据库

本文探讨了在安卓中连接云数据库的两种方法——线程和异步,重点介绍了异步方式,如使用AsyncTask类进行异步数据处理。通过讲解AsynTask的结构、回调逻辑以及数据传递,展示了一个登录demo的实现过程,强调异步处理在安卓应用中的优势。
摘要由CSDN通过智能技术生成

       连接云数据库我想了一下,自己目前就掌握了两种方法,一种是线程,一种是异步。那么哪种方法比较好呢。其实是异步我个人觉得更好,其实异步的连接也是一种线程的方式,不过不够明显。我写了一个登陆的demo.

       先说一下单纯的线程方式:

       

package com.example.myphone;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;



import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	private TextView name;
	private TextView psd;
	private Button login;
	private boolean isLogInSuccess;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        name=(TextView) findViewById(R.id.name);
        psd=(TextView) findViewById(R.id.password);
        login=(Button) findViewById(R.id.login);
        
        name.setText("123");
        psd.setText("123");
        
        login.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			
				
				new LoginThread().start();
			}
		});
        
        
        
        
        
        
        
    }
	Handler handler = new Handler() {
		@SuppressWarnings({ "rawtypes" })
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 1:
				List resultList = new ArrayList();
				resultList = (List) msg.obj;
				System.out.println("resultList-----"+resultList);  
				System.out.println("size:"+resultList.size());

				String returnResult = resultList.get(0).toString();
				System.out.println("returnResult----"+returnResult);
				if (returnResult != null && !returnResult.equals("1")) { // ?

					String resultUserName = resultList.get(1).toString().trim();

					System.out.println("---resultUserName-----"
							+ resultUserName);
					System.out.println("----  result:" + returnResult + "---");
					System.out.println(returnResult.length());

					String s = "login_succeed";
					System.out.println("s:" + s.length());
					if (returnResult.contains("succeed")) {
						isLogInSuccess = true;
						System.out.println(isLogInSuccess);
					} else {
						Toast.makeText(MainActivity.this,
								"您输入的用户名和密码不匹配,请核
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值