Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android用戶登錄客戶端代碼

android用戶登錄客戶端代碼

編輯:關於Android編程

package com.example.denglu;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
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 android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
TextView textView;
static String get;
Handler handler;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		textView=(TextView)findViewById(R.id.textview);
		handler=new Handler(){
			public void handleMessage(android.os.Message msg) {
				
				textView.setText(get);
			};
		};
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				login(a, z);
	Message message=new Message();
	Bundle dataBundle=new Bundle();
	dataBundle.putString(a, b);
	message.setData(dataBundle);
	handler.sendMessage(message);
			}
		}).start();
		
	}
public static void login(String user,String pswd){
	HttpClient httpClient=new DefaultHttpClient();
	String uri=http://10.0.2.2:8080/sessionlogin/great;
	HttpPost httpPost=new HttpPost(uri);
	Listlist=new ArrayList();
	list.add(new BasicNameValuePair(username,user));
	list.add(new BasicNameValuePair(password,pswd));
	try {
		UrlEncodedFormEntity entity=new UrlEncodedFormEntity(list, utf-8);
		httpPost.setEntity(entity);
		try {
			HttpResponse response=httpClient.execute(httpPost);
			if (response.getStatusLine().getStatusCode()==200) {
				InputStream inputStream=response.getEntity().getContent();
				get=tostring(inputStream);
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	} catch (UnsupportedEncodingException e) {
		// TODO Auto-generated catch block
		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;
	}
static String tostring(InputStream inputStream){
	ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
	byte[]buffer=new byte[1024];
	int len=0;
	String a=null;
	try {
		while ((len=inputStream.read(buffer))!=-1) {
			
			byteArrayOutputStream.write(buffer, 0, len);
		}
		inputStream.close();
		byteArrayOutputStream.close();
		a=new String(buffer, utf-8);
		
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return a;
}
}


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved