I am battling with this issue for a long time, dug around Google and SO but still no luck. Finally, I am out here to get your help, please advise or help.
My problem with the following source code is that it only displays string content the images do not display instead it shows white rectangle or sometimes a blue image with question mark.
Q: How to display images?
Here is my code:
private void openURL() {
DefaultHttpClient client = new HttpsRequest(getApplicationContext());
HttpGet get = new HttpGet(getUrlField().getText().toString());
// Execute the GET call and obtain the response
HttpResponse getResponse;
try {
getResponse = client.execute(get);
HttpEntity responseEntity = getResponse.getEntity();
String content = EntityUtils.toString(responseEntity);
getWebView().loadDataWithBaseURL(null, content, "text/html", "utf-8",
null);
} catch (ClientProtocolException e) {
WSLog.e(THIS_FILE, "HTTP Error.");
e.printStackTrace();
} catch (IOException e) {
WSLog.e(THIS_FILE, "Url Load Error.");
e.printStackTrace();
}
}
webView output:
With the help of Vasarat and few modification from my side helped me to answer this question. I have modified the following code line as
getWebView().loadDataWithBaseURL("http://mywebSite.com/parent_dir_to_iamges/", content, "text/html", "UTF-8","about:blank");
This modification gave me the perfect output as expected.
Please fallow the comments to understand details about the issue.
Note: I have used http in the base url instead https.....Please let me know if I can use https. webview.Loadurl() with https url works fine if API level is 10 or above but it shows blank page for API level 8.
Related
I am building a web-scraper using Java and JavaFx. I already have an application running using JavaFx.
I am building a web-scraper following similar procedures as this blog: https://ksah.in/introduction-to-web-scraping-with-java/
However, instead of having a fixed url, I want to input any url and scrape. For this, I need to handle the error when the url is not found. Therefore, I need to display "Page not found" in my application console when the url is not found.
Here is my code for the part where I get URL:
void search() {
List<Course> v = scraper.scrape(textfieldURL.getText(), textfieldTerm.getText(),textfieldSubject.getText());
...
}
and then I do:
try {
HtmlPage page = client.getPage(baseurl + "/" + term + "/subject/" + sub);
...
}catch (Exception e) {
System.out.println(e);
}
in the scraper file.
It seems that the API will throw FailingHttpStatusCodeException if you set it up correctly.
if the server returns a failing status code AND the property
WebClientOptions.setThrowExceptionOnFailingStatusCode(boolean) is set
to true.
You can also get the WebResponse from the Page and call getStatusCode() to get the HTTP status code.
The tutorial you added contains the following code:
.....
WebClient client = new WebClient();
client.getOptions().setCssEnabled(false);
client.getOptions().setJavaScriptEnabled(false);
try {
String searchUrl = "https://newyork.craigslist.org/search/sss?sort=rel&query=" + URLEncoder.encode(searchQuery, "UTF-8");
HtmlPage page = client.getPage(searchUrl);
}catch(Exception e){
e.printStackTrace();
}
.....
With this code when client.getPage throws any error, for example a 404, it will be catched and printed to the console.
As you stated you want to print "Page not found", which means we have to catch a specific exception and log the message. The library used in the tutorial is net.sourceforge.htmlunit and as you can see here (http://htmlunit.sourceforge.net/apidocs/com/gargoylesoftware/htmlunit/WebClient.html#getPage-java.lang.String-) the getPage method throws a FailingHttpStatusCodeException, which contains the status code from the HttpResponse. (http://htmlunit.sourceforge.net/apidocs/com/gargoylesoftware/htmlunit/FailingHttpStatusCodeException.html)
This means we have to catch the FailingHttpStatusCodeException and check if the statuscode is a 404. If yes, log the message, if not, print the stacktrace for example.
Just for the sake of clean code, try not to catch them all (like in pokemon) as in the tutorial but use specific catch-blocks for the IOException, FailingHttpStatusCodeException and MalformedURLException from the getPage method.
Solved
my problem was that my imagelink string was null sometimes, and i was bcz of bad getting data!
I have an image link which i want to download it into bitmap, this but i cant because the bitmapdownloader UniversalImageLoader both shows null, but the link is perfectly correct and working.
Im getting them from a wordpress website which the link is:
http://wikirap.ir/?get_all
i face this problem sometimes for some post photos in website. so i faced it again for a post photo(i mean the imgloaders was showing null) and i decided to put another photo(which it was loading correctly in app) for the post! but guess what? it didnt load too! but when i was opening the other post with same photo(in app) its loading!
this problem is making me crazy badly!
Update:
i changed my bitmap download code but my first code was saying this error
"... Bitmap.getconfig() on null object reference"
Help me to get rid of this please.
The thing i found out is that the photo shows as preview photo in list but when i click to see details it shows blank!(really bullshit)
Update Code :
Json getting : well i get it with okhttp and its ok and no need guys.
Showing in list: I use json and lazy list adapter which is working perfect and showing all photos even those i cant load them in detail page.
(preview image link is the same as detail image link)
detail page code getting bitmap:
protected void onPostExecute(String file_url) {
try {
actionBar.setTitle(name1.toString() + " - " + name2.toString());
tvinfo.setText(likes + " Likes | " + views + " Views");
}catch (NullPointerException e)
{
}
try
{
/**
URL url = new URL(Imagelink);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
bitmaporigin = BitmapFactory.decodeStream(input);
**/
imgLoadercover imglcover = new imgLoadercover(ac.getApplicationContext());
imglcover.DisplayImage(Imagelink, R.color.placeholder, cover);
}catch (Exception e)
{
}
}
The problem my application reads in jpegs and displays them on a jlabel (these are pictures of books)
Everything works fine when used with the local version e.g. reading from the C drive, but once I try to do this over the internet problems occur that I have tried without success to correct
Scenario
Should the jpeg not be present at the end of the url I get the following error
javax.imageio.IIOException: Can't get input stream from URL!
In the version that reads from the local drive I detect if the file exist and overcome this problem however I have tried lots of the ideas posted and I simply can’t find out how to detect that the jpeg is absent!
Please can some one help
here are the two version of the code
Read from local drive C
private void showcover() {
String stockPic;
String partofISBN;
String completeurl;
jButton9.setVisible(true);
stockPic = jTextField1.getText();// get the current isbn
partofISBN = stockPic.substring(0, 7); // get first 7 numbers
String picUrl;
stockPic = stockPic + localNumber + ".jpg";
picUrl = partofISBN + "\\" + stockPic;
completeurl = "C:\\Apicture\\" + picUrl;
File pf = new File(completeurl);
if (!pf.exists()) {
jLabel9.setIcon(new ImageIcon("C:\\Apicture\\" + picUrl));
jLabel9.setIcon(new ImageIcon("C:\\Apicture\\nojpegs.jpg"));
jLabel9.setText("NO Jpeg");
}
jLabel9.setIcon(new ImageIcon(completeurl));
}
Adaption to read from url
URL url;
url = new URL("http://ebid.s3.amazonaws.com/upload_big/9/1/1/1401018425-17770-385.jpg");
Image image = null;
try {
image = ImageIO.read(url);
} catch (IOException ex) {
Logger.getLogger(baseframe.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (MalformedURLException ex) {
Logger.getLogger(baseframe.class.getName()).log(Level.SEVERE, null, ex);
}
javax.imageio.IIOException means your are not getting the image.
So add more code to failover to alternate URL/disk in your catch(IOException ) block.
Read http HEAD request and response code 404 for not found. Http is your friend.
Find out of the resource exists before you GET it.
You may want to check this answer I just provided: I can't download a specific image using java code
Seems like amazon is also checking your user-agent, so you may have to put something like this at the beginning of your code:
System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0");
edit: "at the beginning of your code" really means something like "before creating any URL-related object". I mean, I didn't want to refer to the code you posted, but you whole application.
Use this:
URL url;
url = new URL("http://ebid.s3.amazonaws.com/upload_big/9/1/1/1401018425-17770-385.jpg");
url.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0");
Image image = null;
try {
image = ImageIO.read(url);
} catch (IOException ex) {
Logger.getLogger(baseframe.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (MalformedURLException ex) {
Logger.getLogger(baseframe.class.getName()).log(Level.SEVERE, null, ex);
}
I have a PNR Inquiry app on Google Play. It was working very fine. But recently Indian Railwys added captcha to their PNR Inquiry section and because of this I am not able to pass proper data to the server to get proper response. How to add this captcha in my app in form of an imageview and ask the users to enter captcha details also so that I can send proper data and get proper response.
Indian Railways PNR Inquiry Link
If you check the html code, its actualy pretty bad captcha.
Background of captcha is: http://www.indianrail.gov.in/1.jpg
Those numbers are actualy in input tag:
<input name="lccp_cap_val" value="14167" id="txtCaptcha" type="hidden">
What they are doing is, via javascript, use numbers from that hidden input tag
and put them on that span with "captcha" background.
So basicaly your flow is:
read their html
get "captcha" (lol, funny captcha though) value from input field
when user puts data in your PNR field and presses Get Status
post form field, put PNR in proper value, put captcha in proper value
parse response
Oh yeah, one more thing. You can put any value in hidden input and "captcha"
input, as long as they are the same. They aren't checking it via session or
anything.
EDIT (code sample for submiting form):
To simplify posting form i recommend HttpClient components from Apache:
http://hc.apache.org/downloads.cgi
Lets say you downloaded HttpClient 4.3.1. Include client, core and mime
libraries in your project (copy to libs folder, right click on project,
properties, Java Build Path, Libraries, Add Jars -> add those 3.).
Code example would be:
private static final String FORM_TARGET = "http://www.indianrail.gov.in/cgi_bin/inet_pnstat_cgi.cgi";
private static final String INPUT_PNR = "lccp_pnrno1";
private static final String INPUT_CAPTCHA = "lccp_capinp_val";
private static final String INPUT_CAPTCHA_HIDDEN = "lccp_cap_val";
private void getHtml(String userPnr) {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody(INPUT_PNR, userPnr); // users PNR code
builder.addTextBody(INPUT_CAPTCHA, "123456");
builder.addTextBody("submit", "Get Status");
builder.addTextBody(INPUT_CAPTCHA_HIDDEN, "123456"); // values don't
// matter as
// long as they
// are the same
HttpEntity entity = builder.build();
HttpPost httpPost = new HttpPost(FORM_TARGET);
httpPost.setEntity(entity);
HttpClient client = new DefaultHttpClient();
HttpResponse response = null;
String htmlString = "";
try {
response = client.execute(httpPost);
htmlString = convertStreamToString(response.getEntity().getContent());
// now you can parse this string to get data you require.
} catch (Exception letsIgnoreItForNow) {
}
}
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line);
}
} catch (IOException ignoredOnceMore) {
} finally {
try {
is.close();
} catch (IOException manyIgnoredExceptions) {
}
}
return sb.toString();
}
Also, be warned i didn't wrap this in async call, so you will have to do that.
Image from the network can be displayed in android via efficient image loading api's like Picasso/volley or simply image view via async task.
considering all above things as basic build a logic such that you should need a image URL for that captcha if user resets or refresh the captcha it should reload new image via network call requesting the new request implementation, you have to get REST api access to the Indian railway and check in that any image uri available in that (it may be in base64 format )
if REST API is not available you may think of building your own server with this code
RESTful API to check the PNR Status
pnrapi
Update: you don't need to do this complex hacks , just implement Drago's answer !
I want to share some information in google plus wall from my application.and I am trying for moment.insert, But getting 400 error . Can somebody help me
#Override
public JSONObject getGooglePlusAddUseractivities(Object token) {
Token accessToken = (Token) token;
OAuthService service = createOAuthServiceForGooglePlus();
OAuthRequest request = new OAuthRequest(Method.POST,"https://www.googleapis.com/plus/v1/people/me/moments/vault");
request.addQuerystringParameter("alt", "json");
service.signRequest(accessToken, request);
JSONObject object=new JSONObject();
try {
object.put("kind","plus#moment");
object.put("type","http://schemas.google.com/AddActivity");
JSONObject obj1=new JSONObject();
obj1.put("kind", "plus#itemScope");
obj1.put("url","https://plus.google.com/me");
obj1.put("description","Sign up now to claim and redeem your credits while shopping! ");
obj1.put("image","http://invite.png");
obj1.put("contentUrl", "www.abcd.com");
obj1.put("thumbnailUrl", "http://logo1_favicon.png");
object.putOpt("target", obj1);;
}catch(Exception ex) {
ex.printStackTrace();
}
request.addPayload(object.toString());
request.addHeader("Content-Type", "application/json");
System.out.println("request : "+request.getBodyContents());
Response response = request.send();
String responseBody = response.getBody();
JSONObject googleJSON = null;
try {
googleJSON = new JSONObject(responseBody);
}
catch (JSONException e) {
System.out.println("can not create JSON Object");
}
getting 400 error ?? anyone can tell me..... where am wrong ..!!`
It isn't clear from the documentation, but you can't provide both the target.url and most other target metadata. This is currently opened as bug 485 in the issue tracking system - please go there and star the issue to make sure they properly prioritize a fix.
If you remove the target.url value and add a target.id value, it should work.
(As an aside, this does not post in the user's stream, but will post an App Activity in their app moment vault. They must manually share the activity if they choose.)
At this time, it is not possible to programmatically write to a user's Stream. As a developer, you have two options:
Write an AppActivity (formerly known as a Moment), which writes information to Google, but not to a Google+ Stream. These activities are visible at plus.google.com/apps, and will be used by Google in additional ways over time.
Create an Interactive Post Share button, which a user must initiate. However, you can pre-fill both the text of the post and up to 10 intended recipients. The user can make changes if they want and then perform the actual share. You can learn more at https://developers.google.com/+/web/share/interactive or by watching this Google+ Developers Live episode: http://www.youtube.com/watch?v=U4Iw28jWtAY.