【问题标题】:Android -> AsyncTask, HttpClient and SharedPreferencesAndroid -> AsyncTask、HttpClient 和 SharedPreferences
【发布时间】:2012-09-29 18:30:08
【问题描述】:

我需要有关使用 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();
        }

【问题讨论】:

    标签: android android-asynctask httpclient sharedpreferences


    【解决方案1】:

    您可以从 Link 中按照 AsyncTask 的一些步骤进行操作

    它可能对你有帮助..

    【讨论】:

    • 谢谢。你能告诉我我是否必须在新活动中创建异步任务,或者我可以将它放在我现有的一些活动中(主要活动除外)??
    • 你可以制作内部类或单独的类..这取决于你。
    猜你喜欢
    • 1970-01-01
    • 2016-10-12
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 2011-04-21
    • 2012-12-14
    • 2021-09-27
    相关资源
    最近更新 更多