編輯:關於Android編程
下面為使用HttpClient的一個登錄服務器的小例子
package com.liang.logindemo; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.View; import android.widget.EditText; import android.widget.Toast; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.BufferedHttpEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.Reader; import java.io.StringReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.ArrayList; import java.util.List; public class MainActivity3 extends ActionBarActivity { private EditText et_userName; private EditText et_password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_userName= (EditText) findViewById(R.id.et_userName); et_password= (EditText) findViewById(R.id.et_password); } public void login(View view) { String str=et_userName.getText().toString(); try { str=URLEncoder.encode(str,"utf-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } final String userName=str; final String password=et_password.getText().toString(); //在子線程中訪問網絡 new Thread(new Runnable() { @Override public void run() { try { final boolean isSuccess =loginByPost(userName,password); //final boolean isSuccess = loginByGet(userName,password); //使用此方法可不使用Handler通知主線程,方法內所做操作由主線程完成 runOnUiThread(new Runnable() { @Override public void run() { if(isSuccess){ Toast.makeText(MainActivity3.this,"成功了!!!",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity3.this,"失敗了!!!",Toast.LENGTH_SHORT).show(); } } }); } catch (Exception e) { e.printStackTrace(); } } }).start(); } /** * HttpClient通過GET請求方式訪問服務器 * @param userName,password * @return * @throws Exception */ private Boolean loginByGet(String userName,String password) throws Exception{ //服務器地址 String url="http://192.168.1.140:8080/Login/servlet/Login"; String data="?userName="+userName + "&password="+password; HttpClient client=null; //定義一個客戶端 client = new DefaultHttpClient(); //定義一個get請求 HttpGet get = new HttpGet(url+data); //執行get請求,獲得響應對象 HttpResponse response = client.execute(get); //獲得響應狀態碼 int code = response.getStatusLine().getStatusCode(); //返回結果 if(code==200) { //獲得響應內容 InputStream is = response.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String text = reader.readLine(); reader.close(); is.close(); return "SUCCESS".equals(text) ? true : false; } if(client!=null){ //關閉連接 client.getConnectionManager().shutdown(); } return false; } /** *HttpClient通過POST請求方式訪問服務器 * @param userName * @param password * @return * @throws Exception */ boolean loginByPost(String userName,String password) throws Exception{ //服務器地址 String url="http://192.168.1.140:8080/Login/servlet/Login"; HttpClient client=null; //定義一個客戶端 client = new DefaultHttpClient(); //定義一個Post請求 HttpPost post = new HttpPost(url); //設置請求數據 Listlist = new ArrayList (); list.add(new BasicNameValuePair("userName",userName)); list.add(new BasicNameValuePair("password",password)); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list); post.setEntity(entity); //執行get請求,獲得響應對象 HttpResponse response = client.execute(post); //獲得響應狀態碼 int code = response.getStatusLine().getStatusCode(); //返回結果 if(code==200) { //獲得響應內容 InputStream is = response.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String text = reader.readLine(); reader.close(); is.close(); return "SUCCESS".equals(text) ? true : false; } if(client!=null){ //關閉連接 client.getConnectionManager().shutdown(); } return false; } }
先來看下效果:控件內容比較簡單,就是一個普通的折線圖,上下分別帶有數字,點擊的時候顯示當天溫度的差值。 創建一個類繼承自View,並添加兩個構造方法:publi
在前幾篇文章中《Android 采用get方式提交數據到服務器》《Android 采用post方式提交數據到服務器》《Android 采用HttpClient提交數據到服
本文主要和大家分享如何在Android應用開發過程中如何進行單元測試,個人在做項目的過程中,覺得單元測試很有必要,以保證我們編寫程序的正確性。下面我們先大概了解下單元測試
就像Button控件有監聽器一樣,動畫效果也有監聽器,只需要實現AnimationListener就可以實現對動畫效果的監聽,其中需要重載三個函數,就是下面的這幾個函數: