new Thread(new Runnable() { public void run() { try { HttpClient mHttpClient = new DefaultHttpClient(); //使用post傳值 HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8")); HttpResponse mHttpResponse = mHttpClient.execute(httpPost); //使用Get傳值 HttpGet httpGet = new HttpGet(url); HttpResponse mHttpResponse = mHttpClient.execute(httpGet); // HttpURLConnection.HTTP_OK為200,200代表串接溝通成功 if (mHttpResponse.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) { //資料處理 } // 送訊息跟handler講背景處理完 handler.sendEmptyMessage(0); } catch (Exception e) { e.printStackTrace(); } } }).start();API POST傳值
handler接收到消息後 更新UI
private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { // handler接收到消息後 } };Thread傳自訂Message到Handler
沒有留言:
張貼留言