Autore Topic: Problema Webview Kitkat  (Letto 429 volte)

Offline droidmaniac90

  • Nuovo arrivato
  • *
  • Post: 26
  • Respect: 0
    • Mostra profilo
  • Dispositivo Android:
    Nexus One
  • Sistema operativo:
    Windows 7,Xp,Ubuntu
Problema Webview Kitkat
« il: 21 Febbraio 2014, 22:38:15 CET »
0
Salve, realizzando una app con una webview in cui compaiono delle foto prese da un web server, su android minore di 4.4 le foto si adattano perfettamente allo schermo mentre su android 4.4 kitkat vengono visualizzate zoomate e quindi una porcheria. Facendo una ricerca ho visto che google ha cambiato le webview. Ho anche trovato questo link https://developer.android.com/guide/webapps/migrating.html , ma non capisco cosa dovrei cambiare e per cosa. Qualcuno ha esperienza su questo argomento? qualcuno può darmi una mano? grazie!

ecco il codice:
Codice: [Seleziona]
private JSONParser parser;

    private String pkg;
    private ListView listView1;
    private LinearLayout linearLayout01;

    public ImageLoader imageLoader;

    private ProgressDialog dialog;
    private int width;

    private Post post;

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        imageLoader = new ImageLoader(getApplicationContext());

        pkg = getPackageName();
        Intent intent = getIntent();

        String page = intent.getStringExtra(pkg + "PAGE");
        String title = intent.getStringExtra(pkg + "TITLE");

        setTitle(title);

        parser = new JSONParser();

        listView1 = (ListView)findViewById(R.id.listView1);
        linearLayout01 = (LinearLayout)findViewById(R.id.linearLayout01);

        dialog = new ProgressDialog(this);

        Display display = getWindowManager().getDefaultDisplay();
        width = display.getWidth();

        webView = new WebView(this);

        new LoadSchermataTremore(page).execute();
    }

private class LoadSchermataTremore extends AsyncTask<Void, Void, List<Post>>{

        private Post post;
        private String mPage;
        private List<Post> lstPost = new ArrayList<Post>();

        public LoadSchermataTremore (String page){
            this.mPage = page;
        }

        @Override
        protected List<Post> doInBackground(Void... params) {
            try {


                post = new Post();

                HttpClient httpclient = new DefaultHttpClient(); 
                HttpPost httppost = new HttpPost("http://url.it/?json=get_page&id=" + mPage); //URL address
                Log.v(TAG, "page: http://url.it/?json=get_page&id=" + mPage);
                HttpResponse httpresponse = httpclient.execute(httppost);
                HttpEntity resEntity = httpresponse.getEntity();

                String result = EntityUtils.toString(resEntity);

//              Log.v(TAG, "post: " + result);

                post = parser.postFromJson(result);

//              String htmlText = " %s ";
//             
//              WebView webView = new WebView(getApplicationContext());
//              webView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1));
//              webView.loadData(String.format(htmlText, post.getContent()), "text/html", null);
//             
//              linearLayout01.addView(webView);

                HTMLParser htmlParser = new HTMLParser();
                lstPost = htmlParser.postsFromHtml(post.getContent());

            } catch (Exception e) {
                Log.e(TAG, "LoadSchermataTremore(),Exception: " + e);
            }

             return lstPost;
        }

        @Override
        protected void onPostExecute(List<Post> result) {
            super.onPostExecute(result);

            try {

            Log.v(TAG, "post: " + post.getContent());

            String htmlText = " %s ";


            webView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1));
            webView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
            webView.loadData(String.format(htmlText, post.getContent()), "text/html", null);

            webView.setWebViewClient(new WebViewClient(){
//              @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    Log.d(TAG, "url: " + url);
                    Intent share = new Intent(getApplicationContext(), ShareActivity.class);
                    share.putExtra(pkg + "LINK", url);

                    startActivity(share);
                    return true;
                  }
            });

            linearLayout01.addView(webView);

        } catch (Exception e) {
            Log.e(TAG, "LoadSchermataTremore(),Exception: " + e);
        }


//          WebAdapter webAdapter = new WebAdapter(LiveActivity.this);
//         
//          for (int i = 0; i < result.size(); i++) {
//              webAdapter.add(result.get(i));
//          }
//         
//          listView1.setAdapter(webAdapter);
//         
            try {
                if (dialog.isShowing()) {
                    dialog.dismiss();
                    dialog = null;
                }
            } catch (Exception e) {

            }

        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            dialog.setMessage("Loading.....");
            dialog.show();
        }
    }

private class WebAdapter extends ArrayAdapter<Post>{
    private final LayoutInflater mLayoutInflater;

    public WebAdapter(LiveActivity activity) {
        super(activity, 0);
        mLayoutInflater = LayoutInflater.from(activity);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {

            convertView = mLayoutInflater.inflate(R.layout.row_web, parent, false);

            holder = new ViewHolder();
            holder.tvTitolo = (TextView) convertView.findViewById(R.id.txtRowWeb);
            holder.wvPost = (WebView) convertView.findViewById(R.id.wvImgWeb);
            holder.ivWeb = (ImageView) convertView.findViewById(R.id.ivWeb);
            holder.tvlink = (TextView) convertView.findViewById(R.id.tvLink);

            convertView.setTag(holder);

        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        final Post post = getItem(position);

        holder.tvTitolo.setText(post.getTitle());


        holder.wvPost.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);

        holder.wvPost.loadUrl(post.getThumbnail());

        holder.wvPost.setFocusable(false);
        holder.wvPost.setClickable(false);

        holder.tvlink.setText(post.getUrl());

        return convertView;
    }
}

private String generateHTLM(String title,String image){
    String str = "";
    str = "<link rel='stylesheet' href='http:urlcouraselcss.css' />" +
          "<script src='http://code.jquery.com/jquery-1.9.0.min.js'></script>" +
          "<script src='http://url'></script>" +
          "<script type='text/javascript'>" +

          "</div>" +
          "</div>" +
          "</div>" +
          "</div>" +
          "<br>" +
          "<br>" +
          "<br>" +
          "</body>";

    return str;
}

private static class ViewHolder {

    TextView tvTitolo;
    TextView tvlink;
    ImageView ivWeb;
    TextView tvContent;
    WebView wvPost;
    TextView tvIDPost;
}

}
« Ultima modifica: 22 Febbraio 2014, 11:45:28 CET da droidmaniac90 »