0

我需要有关使用 AsyncTask 的帮助。我正在使用 HttpClient post 将数据(保存在 SharedPreferences 中)发送到网页。我需要在 AsyncTask 中编写它。有人可以通过我的示例代码帮我配置它吗?

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();
        }
4

1 回答 1

0

您可以从此链接遵循 AsyncTask 的一些步骤

它可能对你有帮助..

于 2012-09-29T18:35:52.637 回答