1 / 5

HTTP kommunikáció Androidon

HTTP kommunikáció Androidon. HttpClient-en keresztűl HttpPost/HttpGet objektum használatával HttpClient execute metódusának meghívása. HttpPost. HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(SERVER_ADDRESS); try {

burke
Download Presentation

HTTP kommunikáció Androidon

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. HTTP kommunikáció Androidon • HttpClient-en keresztűl • HttpPost/HttpGet objektum használatával • HttpClient execute metódusának meghívása

  2. HttpPost HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(SERVER_ADDRESS); try { List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("text", texts[0])); httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse response = httpClient.execute(httpPost); } catch (Exception e) { e.printStackTrace(); } Ez így már működik, de van vele egy kis gond...

  3. UI Thread blokkolása • Nem szerencsés • User-t zavarja • Rendszer kilőheti az appot (ANR) • Hálózati kommunikáció nem megengedett • Többféle megoldás • Runnable – runOnUIThread • AsyncTask

  4. AsyncTask • Automatikusan kezeli a szálváltást • Saját osztályt kell belőle származtatni • Meg van a saját életciklusa • doInBackground • Háttérben fut • OnPostExecute • UI Thread-en fut

  5. AsyncTask példa private class SendTask extends AsyncTask<String, Void, Void> { @Override protected Void doInBackground(String... texts) { HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(SERVER_ADDRESS); try { List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("text", texts[0])); httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse response = httpClient.execute(httpPost); } catch (Exception e) { e.printStackTrace(); } return null; } } Futtatás: new SendTask().execute(new String[] { param });

More Related