2012-09-29 45 views
0

我需要使用AsyncTask的幫助。我正在使用HttpClient後發送數據(這是保存在SharedPreferences)到網頁。我需要在AsyncTask中編寫這個內容...有人可以幫我通過我的示例代碼進行配置。Android - > AsyncTask,HttpClient和SharedPreferences

SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE); 
     String email2 = sharedPreferences.getString("EMAIL", ""); 
     String lozinka2 = sharedPreferences.getString("LOZINKA", ""); 
     sprEmail2.setText(email2); 
     sprLozinka2.setText(lozinka2); 

     try { 
      HttpClient client = new DefaultHttpClient(); 
      String postURL = "https://m.bonbon.hr/user/login"; 
      HttpPost post = new HttpPost(postURL); 
      post.setHeader("Content-type", "application/x-www-form-urlencoded"); 
       List<NameValuePair> params = new ArrayList<NameValuePair>(); 
       params.add(new BasicNameValuePair("email", email2)); 
       params.add(new BasicNameValuePair("password", lozinka2)); 
       params.add(new BasicNameValuePair("lsend", "Prijavi se")); 
       UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8); 
       post.setEntity(ent); 
       HttpResponse responsePOST = client.execute(post); 
       HttpEntity resEntity = responsePOST.getEntity(); 
       if (resEntity != null) {  
        rezz=EntityUtils.toString(resEntity); 
        response2.setText(rezz); 
        Log.i("rezz",rezz); 

        WebView webView = new WebView(this); 
        setContentView(webView); 
        webView.loadData(rezz, "text/html", "utf-8"); 
       } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

回答

0

你可以遵循從這個Link

它可能對你有幫助..

+0

感謝您的AsyncTask的一些步驟。你能告訴我我是否必須在新活動中創建asynctask,或者我可以將它放在我現有的一些活動中(免除主要活動)? – Goran

+0

你可以讓內部課程或單獨的課程..它取決於你。 –