I am trying to upload image in CKEditor with image2 plugin.It is wokring fine when I am uploading it by 'send it to server' button but drag and drop of images is not working.
Below is Version Detail:
- Primefaces 6.0
- Ckeditor 4.9.1
- JSF 2.2
Config.js
CKEDITOR.editorConfig = function(config){
// config.timestamp='ABCD';
config.disableNativeSpellChecker = false;
config.pasteFromWordRemoveFontStyles=false;
config.pasteFromWordRemoveStyles=false;
config.allowedContent=true;
config.filebrowserBrowseUrl='#{request.contextPath}/CKEditorGetImageServlet';
config.filebrowserUploadUrl = 'CKEditorUploadServlet';
//config.filebrowserUploadMethod = "form";
config.uploadUrl='CKEditorUploadServlet';
config.extraPlugins = 'image2';
};
Upload Servlet:
public class CKEditorUploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String ERROR_FILE_UPLOAD = "An error occurred to the file upload process.";
private static final String ERROR_NO_FILE_UPLOAD = "No file is present for upload process.";
private static final String ERROR_INVALID_CALLBACK = "Invalid callback.";
private static final String CKEDITOR_CONTENT_TYPE = "text/html; charset=UTF-8";
private static final String CKEDITOR_HEADER_NAME = "Cache-Control";
private static final String CKEDITOR_HEADER_VALUE = "no-cache";
private static final Pattern PATTERN = Pattern.compile("[\\w\\d]*");
private String errorMessage = "";
public CKEditorUploadServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CKEDITOR_CONTENT_TYPE);
response.setHeader(CKEDITOR_HEADER_NAME, CKEDITOR_HEADER_VALUE);
final FileItemFactory factory = new DiskFileItemFactory();
final ServletFileUpload upload = new ServletFileUpload(factory);
List fileItems = null;
Iterator itr = null;
FileItem fi = null;
File file = null;
String contentType = "";
byte[] content;
String id = UUID.randomUUID().toString().substring(0, 8);
System.out.println(id);
final PrintWriter out = response.getWriter();
UploadBean bean = (UploadBean) getServletContext().getAttribute("uploadBean");
if (bean == null) {
bean = new UploadBean();
getServletContext().setAttribute("uploadBean", bean);
}
try {
/*final List items = upload.parseRequest(request);
if (!items.isEmpty() && items.get(0) != null) {
System.out.println("Content "+((DiskFileItem) items.get(0)).get());
System.out.println("Content Type "+((DiskFileItem) items.get(0)).getContentType());
System.out.println("Name "+((DiskFileItem) items.get(0)).getName());
} else {
//No file to Upload
}*/
fileItems = upload.parseRequest(request);
itr = fileItems.iterator();
while ( itr.hasNext () )
{
fi = (FileItem)itr.next();
if ( !fi.isFormField () )
{
String fileName = fi.getName();
System.out.println("FileName "+fileName);
String ext = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length());
boolean isInMemory = fi.isInMemory();
contentType = fi.getContentType();
content = fi.get();
bean.setContent(fi.get());
long sizeInBytes = fi.getSize();
if( fileName.lastIndexOf("\\") >= 0 )
{
file = new File("D:\\Document" + "\\" + id + "." + ext) ;
}
else
{
file = new File("D:\\Document\\" + "\\" + id + "." + ext) ;
}
fi.write( file ) ;
}
}
bean.setId(id);
bean.setContentType(contentType);
} catch (Exception e) {
e.printStackTrace();
}
finally
{
fileItems = null;
itr = null;
fi = null;
}
// CKEditorFuncNum Is the location to display when the callback
// String callback = request.getParameter("CKEditorFuncNum");
String callback ="1";
System.out.println("Callback "+callback);
// verify if the callback contains only digits and letters in order to
// avoid vulnerability on parsing parameter
if (!PATTERN.matcher(callback).matches()) {
System.out.println("Patten not matches");
callback = "";
errorMessage = ERROR_INVALID_CALLBACK;
}
final String pathToFile = request.getContextPath()
+ "/CKEditorGetImageServlet?imageId=" + bean.getId();
System.out.println("Path to File "+pathToFile);
out.println("<script type='text/javascript'>window.parent.CKEDITOR.tools.callFunction("
+ callback + ",'" + pathToFile + "','" + errorMessage + "')");
out.println("// ]]></script>");
out.flush();
out.close();
}
}
GetImage Servlet
public class CKEditorGetImageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String ERROR_FILE_DOWNLOAD = "An error occured when trying to get the image with id:";
private static final String IMAGE_PARAMETER_NAME = "imageId";
private static final long CACHE_AGE_MILISECONDS_TWO_WEEKS = 1209600000;
private static final String CKEDITOR_CONTENT_EXPIRE = "Expires";
private static final String CKEDITOR_CONTENT_TYPE = "Content-Type";
private static final String CKEDITOR_CONTENT_LENGTH = "Content-Length";
private static final String CKEDITOR_CONTENT_DISPOSITION = "Content-Disposition";
private static final String CKEDITOR_CONTENT_DISPOSITION_VALUE = "inline; filename=\"";
private static final String CKEDITOR_HEADER_NAME = "Cache-Control";
public CKEditorGetImageServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
UploadBean bean = (UploadBean) getServletContext().getAttribute("uploadBean");
final byte[] rb = bean.getContent();
System.out.println("Inside get Image Servlet "+rb);
System.out.println("Content Type "+bean.getContentType());
final long expiry = new Date().getTime()
+ CACHE_AGE_MILISECONDS_TWO_WEEKS;
response.setDateHeader(CKEDITOR_CONTENT_EXPIRE, expiry);
response.setHeader(CKEDITOR_HEADER_NAME, "max-age="
+ CACHE_AGE_MILISECONDS_TWO_WEEKS);
response.setHeader(CKEDITOR_CONTENT_TYPE,
bean.getContentType());
response.setHeader(CKEDITOR_CONTENT_LENGTH,
String.valueOf(rb.length));
response.setHeader(
CKEDITOR_CONTENT_DISPOSITION,
CKEDITOR_CONTENT_DISPOSITION_VALUE
+ bean.getName() + "\"");
response.getOutputStream().write(rb, 0, rb.length);
response.getOutputStream().flush();
response.getOutputStream().close();
}
As per the Official Documentation of ckeditor we need to write the xhr code at editor level and also at server level which I am not able to get. Could you please help me out in achieving the same.
Related
I have a question now i try create a music app on my phone. now I can see my file song But the name shown in the app is the file address instead. and if i want to play the file music in this app. What code do I need to add for get the name of music and play it on my phone? thank a lot
#Override
public void onClick(View view) {
mp.pause();
play.setEnabled(true);
stop.setEnabled(true);
pause.setEnabled(false);
}
});
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mp != null) {
mp.stop();
try {
mp.prepare();
} catch (IOException e) {
e.printStackTrace();
}
//mp.release()
play.setEnabled(true);
play.setEnabled(true);
stop.setEnabled(false);
}
}
});
send.setOnClickListener(new View.OnClickListener() {
//List<Intent> intentShareList = new ArrayList<Intent>();
#Override
public void onClick( View view ) {
msgs = msg.getText().toString();
System.out.print("msgs " + msgs);
// make line message
Log.d(TAG, "Txt " + msgs);
Intent shareIntent = new Intent();
String userId = "";
String sendText = "line://ti/p/~" + userId;
//shareIntent = null;
try {
shareIntent = Intent.parseUri(sendText,
Intent.URI_INTENT_SCHEME);
} catch (URISyntaxException e) {
e.printStackTrace();
}
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.putExtra(Intent.EXTRA_TEXT,msgs);
shareIntent.setType("text/plain");
startActivity(shareIntent);
}
});
}
AND
public class Songs {
//private
private String songTitle;
private String songAddress;
public Songs(String title){
//songID = id;
songTitle = title;
}
/*public long getSongID(){
return songId;
*/
public String getSongTitle(){
return songTitle;
}
}
now I can see my file song But the name shown in the app is the file
address instead
I think you are referring to the "Path" of the file, for example:
/storage/emulated/0/song.mp3 (Unix style path)
C:\Songs\song.mp3 (Window style path)
If you would like to parse (find out) the name of the file, use the following code:
String filePath = "/storage/emulated/0/song.mp3";
File f = new File(filePath);
String fileName = f.getName();
// fileName == "song.mp3"
In your case:
public class Songs {
private String title; // renamed from "songTitle"
private String path; // renamed from "songAddress"
private String fileName; // the file name of the song
public Songs(String title, String path){
this.title = title;
this.path = path;
File file = new File(path);
String fileName = f.getName();
this.fileName = fileName;
}
public String getTitle(){
return title;
}
public String getPath(){
return path;
}
public String getFileName(){
return fileName;
}
}
I'm trying to read a line of text from a URL and display the result in a Textview.
ISSUE/ERROR:
At present there is no error, the textview is not populated after I execute DownloadWebPage Async task.
INVESTIGATIONS
I believe I may not be calling this correctly, as I can't find any results from my debug calls, in DownloadWebPage. Also my URL has one line of code with two strings, if that makes any difference.
UPDATE:
In DownloadWebPage my logs commands are not showing in the log files - weird
CODE: ON CREATE:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.inbox_list);
textView = (TextView) findViewById(R.id.textView6);
// Hashmap for ListView
inboxList = new ArrayList<HashMap<String, String>>();
//ASYNC TASK I CREATED
new DownloadWebPageTask().execute();
// Another Async Task in my code
new LoadInbox().execute();
}
CODE: AsyncTask for Parsing Data from URL & Setting Data to Textview
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... urls) {
String response = "";
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(content));
String s = "";
while ((s = buffer.readLine()) != null) {
response += s;
}
System.out.println(response);
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}
#Override
protected void onPostExecute(String result) {
textView.setText(Html.fromHtml(result));
}
}
public void readWebpage(View view) {
DownloadWebPageTask task = new DownloadWebPageTask();
task.execute(new String[] { "http://www.url.com/blah.php" });
}
I want to inject javascript file to my site. My site is a simple html page that is on server. I have injected css file. (with Manish's help)
So I can manage my simple html site with CSS now. But I want to manage it with javascript too. My jscript.js file is in asset folder. I want to have full access of javascript on my site. (Remember that, it is MY site) . please write the correct codes for me. Thankx.
Here is my MainActivity.java file:
package com.example.z5070.myapplication;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Base64;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import java.io.InputStream;
public class MainActivity extends ActionBarActivity {
WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = new WebView(this);
setContentView(webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
injectCSS();
super.onPageFinished(view, url);
}
});
webView.loadUrl("http://www.example.com/");
}
private void injectCSS() {
try {
InputStream inputStream = getAssets().open("style.css");
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
inputStream.close();
String encoded = Base64.encodeToString(buffer, Base64.NO_WRAP);
webView.loadUrl("javascript:(function() {" +
"var parent = document.getElementsByTagName('head').item(0);" +
"var style = document.createElement('style');" +
"style.type = 'text/css';" +
"style.innerHTML = window.atob('" + encoded + "');" +
"parent.appendChild(style)" +
"})()");
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Add a new method to inject javascript file.
private void injectJS() {
try {
InputStream inputStream = getAssets().open("jscript.js");
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
inputStream.close();
String encoded = Base64.encodeToString(buffer, Base64.NO_WRAP);
webView.loadUrl("javascript:(function() {" +
"var parent = document.getElementsByTagName('head').item(0);" +
"var script = document.createElement('script');" +
"script.type = 'text/javascript';" +
"script.innerHTML = window.atob('" + encoded + "');" +
"parent.appendChild(script)" +
"})()");
} catch (Exception e) {
e.printStackTrace();
}
}
Call both methods: injectCSS() and injectJS() after page finishes loading.
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
injectCSS();
injectJS();
super.onPageFinished(view, url);
}
});
I hope this solves the problem.
Be wary of how onload events defined inside inject js file would behave.
Some refinements to previous answer. Suppose we use Cyrillic words. Result would be a garbaged strings. It's not good. With code below you can use non-english chars in content. Just add additional url-encoding/decoding to your code and you good to go. Reedited version below.
private void injectJS() {
try {
InputStream inputStream = getAssets().open("jscript.js");
byte[] buffer = new byte[inputStream.available()];
inputStream.read(buffer);
inputStream.close();
// preserve non-english letters
String uriEncoded = URLEncoder.encode(new String(buffer, "UTF-8"), "UTF-8").replace("+", "%20");
String encoded = Base64.encodeToString(uriEncoded.getBytes(), Base64.NO_WRAP);
webView.loadUrl("javascript:(function() {" +
"var parent = document.getElementsByTagName('head').item(0);" +
"var script = document.createElement('script');" +
"script.type = 'text/javascript';" +
// don't forget to use decodeURIComponent after base64 decoding
"script.innerHTML = decodeURIComponent(window.atob('" + encoded + "'));" +
"parent.appendChild(script)" +
"})()");
} catch (Exception e) {
e.printStackTrace();
}
}
could someone please help me. I'm having difficulty with LoadUrl and javascript.
The code below seems to work sorta ok (killing the app then trying again makes it work) on my old HTC running 2.3 however, my Xperia Z which runs the latest Android gets stuck on "Attempting to get Comments".
The section of code it is sticking on is InjectJavaScript() wherein this fails to LoadUrl (I know this as onPageFinished is never called). The strange thing is statistics part of the code works fine and its identical.
Sorry its pretty messy code...
#SuppressLint("SetJavaScriptEnabled")
public class MyIceland extends SherlockActivity {
private static class FetchMyIceland extends AsyncTask<Void, Void, Void>{
#Override
protected Void doInBackground(Void... arg0) {
setDialog("Checking Farera for WoW Data....",2);
switch(FETCHFROMMYICELAND){
case 0: //contact Farera for information.
Log.i("IcePlan","Farera Check/Empathica Front Page Retrieval...");
MyIceland.url = MainActivity.BASE_URL+"android_get.php?action=myiceland&store="+MainActivity.STORE;
try {
String Result = HTTPGET();
if(Result.startsWith("no")){ //content for today?
setDialog("No WoW Data Available for Today\nAbout to contact Empathica.com....",2);
activity.runOnUiThread(new Runnable(){#Override public void run() {MyIceland.FetchFromEmpathica("http://www.iceland.empathica.com/LogonIceland.aspx");;}}); }
else{FETCHFROMMYICELAND = 100;} //we do have content...
} catch (IOException e1) {Log.i("IcePlan",e1.toString());} catch (Exception e1) {Log.i("IcePlan",e1.toString());
} //we have data for today, so download and sort it.
break;
case 1: //get comments
setDialog("About to Contact Empathica.com for Comments....",2);
try {Thread.sleep(1000);} catch (InterruptedException e) {Log.i("IcePlan",e.toString());}
activity.runOnUiThread(new Runnable(){#Override public void run() {MyIceland.FetchFromEmpathica("http://v2.empathica.com/Reporting1/ClientReporting/LiveDataReporting.aspx?RID=1&PID=Comments&TID=1a");}});
break;
case 2: //POST data
setDialog("Posting all data to Farera.com \n So we don't have to do this again today....",2);
List<NameValuePair> params = new ArrayList<NameValuePair>();
try {params.add(new BasicNameValuePair("frontpage", URLEncoder.encode(FRONTPAGE, "utf-8")));} catch (UnsupportedEncodingException e) {Log.e("IcePlan",e.toString());}
try {params.add(new BasicNameValuePair("comments", URLEncoder.encode(COMMENTS, "utf-8")));} catch (UnsupportedEncodingException e) {Log.e("IcePlan",e.toString());}
MyIceland.url = MainActivity.BASE_URL+"android_get.php?action=store_myiceland&store="+MainActivity.STORE;
String POST = HTTPPOST(params);
if(POST != null){FETCHFROMMYICELAND = 100; }
break;
}
return null;
}
#Override
protected void onPreExecute(){super.onPreExecute();}
#Override
protected void onPostExecute(Void arg0){
if(FETCHFROMMYICELAND == 100) new MyIceland.DisplayMyIceland().execute();
}
}
//------------------------------------------------------
//-----------------------------------------------------
//-----------------------------------------------------
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_iceland);
ctx = getApplicationContext();
activity = this;
//webView2 = (WebView) findViewById(R.id.myIwebview2_widget);
/********************************************************/
/****************** ABS ********************************/
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().show();
// This is a workaround for http://b.android.com/15340 from
// http://stackoverflow.com/a/5852198/132047
BitmapDrawable bg = (BitmapDrawable) getResources().getDrawable(
R.drawable.bg_striped);
bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
getSupportActionBar().setBackgroundDrawable(bg);
BitmapDrawable bgSplit = (BitmapDrawable) getResources().getDrawable(
R.drawable.bg_striped);
bgSplit.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
getSupportActionBar().setSplitBackgroundDrawable(bgSplit);
/************************************************************/
try {
ov_e = (TextView) findViewById(R.id.overall_exp_tx);
ov_r= (TextView) findViewById(R.id.overall_rank_tx);
s_e= (TextView) findViewById(R.id.storeexp_tx);
st_e= (TextView) findViewById(R.id.staffexp_tx);
t_e= (TextView) findViewById(R.id.tillexp_tx);
res= (TextView) findViewById(R.id.responses_tx);
res_r= (TextView) findViewById(R.id.resp_rank_tx);
MyIceland.list = (ListView) findViewById(R.id.wow_lv); // set list
MyIceland.adapter = new SeparatedListAdapter(ctx, false); // initialise adapter
//authenticate
CookieSyncManager.createInstance(this);
SetUp(); //Set up webView
setDialog("Loading MyIceland data...\n Please be Patient...",0);
new FetchMyIceland().execute(); //get data
} catch (Exception e) {
// TODO Auto-generated catch block
Log.i("IcePlan",e.toString());
}
}
private static String HTTPPOST(List<NameValuePair> params){
String Resp = null;
try {
HttpClient client = new DefaultHttpClient();
String postURL = MyIceland.url;
HttpPost post = new HttpPost(postURL);
//List<NameValuePair> params = new ArrayList<NameValuePair>();
//params.add(new BasicNameValuePair("user", "kris"));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
Resp = "not null";
}
} catch (Exception e) {
Log.e("IcePlan HTTPPOST",e.toString());
}
return Resp;
}
private static String HTTPGET() throws Exception, IOException{
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null)
{
str.append(line);
}
in.close();
Log.i("IcePlan",str.toString());
return str.toString();
}
private void SetUp(){
webView = (WebView) findViewById(R.id.myIwebview_widget); //load custom webview
webView.clearView(); //clear view of browser
webView.clearFormData(); //clear form data
webView.setNetworkAvailable(MainActivity.isOnline(ctx)); //set network state
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setPluginsEnabled(false); //no flash
webView.getSettings().setJavaScriptEnabled(true); // enable javascript
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
webView.getSettings().setSavePassword(false); //don't show password dialog
webView.setVerticalScrollBarEnabled(false); //no scrolling!
webView.setHorizontalScrollBarEnabled(false); //no scrolling!
webView.getSettings().setLoadsImagesAutomatically(false); // NO Images
webView.getSettings().setSaveFormData(false); //Don't save form data
//webView.setVisibility(View.VISIBLE);
return;
}
private static void FetchFromEmpathica(String EmpathURL){
/*****************************************************
* setWebViewClient to load pages
*****************************************************
*****************************************************
*/
webView.loadUrl(EmpathURL);
MyIceland.page = 0;
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//Log.i("IcePlan",url);
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.i("IcePlan","Starting...");
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
Log.i("IcePlan","Loaded...");
switch(FETCHFROMMYICELAND){
case 0:
if(MyIceland.page == 0){
MyIceland.page=1;
try {Thread.sleep(500);}catch (InterruptedException e) {Log.e("IcePlan",e.toString());}
view.loadUrl("javascript: {" +
"document.forms[0].elements['userNameBox'].value = 'xxx.xxxxx';" +
"setTimeout(function(){document.forms[0].elements['passwordBox'].value = 'xxxxxxx';},300);" +
"setTimeout(function(){document.forms[0].elements['loginButton'].click();},300); };");
}else if(MyIceland.page == 1){
MyIceland.page=2;
try {Thread.sleep(1000);}catch (InterruptedException e) {Log.e("IcePlan",e.toString());}
String oAuthUrl=("v2.empathica.com");
if(url.indexOf(oAuthUrl) > -1)
{
Log.i("Contains","Auth URL");
InjectJavaScript(view);
}else{view.loadUrl("javascript:{};");}
}else if (MyIceland.page ==2){
if(MyIceland.FRONTPAGE == null){
Log.i("IcePlan","Inject");
InjectJavaScript(view);
}else{MyIceland.page=3;}
}
break;
case 1:
if(MyIceland.COMMENTS == null){
Log.i("IcePlan","Inject Comments");
InjectJavaScript(view);
}else{MyIceland.page=100;} //else end
break;
}
super.onPageFinished(view, url);
}
});
return;
}
}
private static void setDialog(final String msg, final int dothis){
activity.runOnUiThread(new Runnable() {
#Override
public void run() {
switch(dothis){
case 0: //set up and show
if (dlg == null) {
dlg = new ProgressDialog(activity);
dlg.setTitle("Loading...");
dlg.setMessage(msg);
dlg.setCancelable(activity.isFinishing());
dlg.show();
}
break;
case 1: //remove and reset to null
if (dlg.isShowing() || dlg!=null) {
dlg.hide();
dlg = null;
}
break;
case 2:
if (dlg.isShowing() || dlg!=null) {
dlg.setMessage(msg);
}
}
}});
return;
}
/*********************************************************
* HACK - Inject Javascript to get the HTML we want!
*********************************************************/
private static void InjectJavaScript(WebView view){
switch(FETCHFROMMYICELAND){
case 0: //get front page
setDialog("Attempting to get Statistics...",2);
view.loadUrl("javascript:{" +
"var element = document.getElementById('ReportDivFPRank');"+
"if (typeof(element) != 'undefined' && element != null)"+
"{" +
"var e=document.getElementById(\"ReportDivFPCR\");"+
"var content=e.innerHTML;"+
"window.HTMLOUT.processHTML(content);"+// exists.
"}" +
"};");
break;
case 1: //get comments
setDialog("Attempting to get Comments....",2);
view.loadUrl("javascript:{" +
"var element = document.getElementById('AddNPSDetails');"+
"if (typeof(element) != 'undefined' && element != null)"+
"{" +
"var e=document.getElementById(\"_ctl1_ReportDiv\");"+
"var content=e.innerHTML;"+
"window.HTMLOUT.processHTML(content);"+// exists.
"}" +
"};");
break;
}
return;
}
/*********************************************************
* Class injected into Empathica website to get HTML
* Continues onto our controlling AsyncTask
* #author Vince
*
********************************************************/
public static class MyJavaScriptInterface
{
public void processHTML(final String html)
{
setDialog("Caught RAW data....",2);
if(html != ""){
//MyIceland.webView2.loadDataWithBaseURL("notreal/", html, "text/htm", "utf-8",null);
if(FETCHFROMMYICELAND == 0){ setDialog("Retrieved Area Statistics....",2); MyIceland.FRONTPAGE = html;}
else if(FETCHFROMMYICELAND == 1){ setDialog("Retrieved Area Comments....",2); MyIceland.COMMENTS = html;}
FETCHFROMMYICELAND++;
new MyIceland.FetchMyIceland().execute();
}
return;
}
}
}
I see that FetchFromEmpathica is going to be called again when you load the second page, and you set again a new WebViewClient() to the webview, put it on the setup() method so it is going to be called once.
Make sure that your Xperia is 4.1.2, nonetheless look what is mentioned on
Reference: JavascriptInterface methods in WebViews must now be annotated
Beginning in Android 4.2, you will now have to explicitly annotate public methods with #JavascriptInterface in order to make them accessible from hosted JavaScript. Note that this also only takes effect only if you have set your app's minSdkVersion or targetSdkVersion to 17 or higher.
Add it and import android.webkit.JavascriptInterface
So the code will be up-to-date to last versions.
#JavascriptInterface
public void processHTML(final String html)
{
//code
}
I am using 8 - 10 different WebViews in one layout and loading different content in each WebView.
While loading Webview shows different messages like "Loading.." "Processing.." etc.
Is there any way to hide these notifications?
Try to use HttpClient to get the webpage's html code and then use WebView.loadData to load the entire page into WebView.
private class exampleHttpTask extends AsyncTask<Integer, Integer, String> {
public String convertStreamToString(InputStream is, String charset) throws IOException {
if (is != null) {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(is, charset));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
return writer.toString();
} else {
return "";
}
}
protected String doInBackground(Integer... params) {
String r = "";
try {
HttpClient hc = new DefaultHttpClient();
HttpGet get = new HttpGet("http://google.com"); // replace with the url
HttpResponse hr = hc.execute(get);
if(hr.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
InputStream is = hr.getEntity().getContent();
r = convertStreamToString(is, "UTF-8");
} else {
r = "Error";
}
} catch(Exception e){
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String result) {
WebView wv = (WebView) findViewById(R.id.web_view); // replace web_view with the webView id
wv.loadData(result, "text/html", "utf-8");
}
protected void onPreExecute() {
}
}
Then call new exampleHttpTask().exec() to load the webpage.