編輯:關於Android編程
final String path = "http://192.168.1.103/Web/servlet/CheckLogin?name=" + name + "&pass=" + pass;
URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setReadTimeout(5000); conn.setConnectTimeout(5000); if(conn.getResponseCode() == 200){ }
String path = "http://192.168.1.103/Web/servlet/CheckLogin?name=" + URLEncoder.encode(name) + "&pass=" + pass;
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { Toast.makeText(MainActivity.this, (String)msg.obj, 0).show(); } }; public void click(View v){ EditText et_name = (EditText) findViewById(R.id.et_name); EditText et_pass = (EditText) findViewById(R.id.et_pass); final String name = et_name.getText().toString(); final String pass = et_pass.getText().toString(); Thread t = new Thread(){ @Override public void run() { //提交的數據需要經過url編碼,英文和數字編碼後不變 @SuppressWarnings("deprecation") String path = "http://192.168.13.13/Web2/servlet/LoginServlet?name=" + URLEncoder.encode(name) + "&pass=" + pass; try { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(5000); conn.setReadTimeout(5000); if(conn.getResponseCode() == 200){ InputStream is =conn.getInputStream(); String text = Utils.getTextFromStream(is); Message msg = handler.obtainMessage(); msg.obj = text; handler.sendMessage(msg); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; t.start(); } }
public static String getTextFromStream(InputStream is){ byte[] b = new byte[1024]; int len = 0; //創建字節數組輸出流,讀取輸入流的文本數據時,同步把數據寫入數組輸出流 ByteArrayOutputStream bos = new ByteArrayOutputStream( ); try { while((len = is.read(b)) != -1){ bos.write(b, 0, len); } //把字節數組輸出流裡的數據轉換成字節數組 String text = new String(bos.toByteArray( )); return text; } catch (IOException e) { e.printStackTrace(); } return null; }
學習技術最好的方式就是實戰,看書看不到的東西太多了,實際操作時會碰到各種書本裡提不到的問題,解決這些問題會迅速提升你的能力,你是一個solider,最好成長的方式就是實戰
一、需求分析“位置分享”App正如其名稱所述那樣,是一款可以分享自己地理位置的應用。“位置分享”App的功能需求大致如下:
項目中需要在應用從後台切換到前台時做操作,自己實現了功能,但對這塊的機制不太了解,So.找了相關的資料來學習總結下。!!!部分資料來源https://github.com
前言對於內存洩漏,我想大家在開發中肯定都遇到過,只不過內存洩漏對我們來說並不是可見的,因為它是在堆中活動,而要想檢測程序中是否有內存洩漏的產生,通常我們可以借助LeakC