I am trying to fetch a url with jsoup in order to download an imadge from that url for some reason it dosn't work.
i am trying first to find where " div class="rg_di" " appears in the html file for the first time, and than to fetch the url that comes right after:
a href="http://www.google.co.il/imgres?imgurl=http://michellepicker.files.wordpress.com/2011/03/grilled-chicken-mexican-style.jpg&imgrefurl=http://michellepicker.wordpress.com/2011/04/25/grilled-chicken-mexican-style-black-beans-guacamole/&h=522&w=700&tbnid=4hXCtCfljxmJXM:&zoom=1&docid=ajIrwZMUrP5_GM&ei=iVOqVPmDDYrnaJzYgIAM&tbm=isch"
this is the url of the html:
here is the code i tried:
try
{
doc = Jsoup.connect(url).get();
Element link = doc.select("div.rg_di").first();
Element link2 = link.select("a").first();
String relHref = link2.attr("href"); // == "/"
String absHref = link.attr("abs:href");
tmpResult = absHref;
}
catch (Exception e)
{
Log.e("Error", e.getMessage());
e.printStackTrace();
}
full activity code:
package com.androidbegin.parselogintutorial;
import com.androidbegin.parselogintutorial.SingleRecipe.urlTask;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.koushikdutta.urlimageviewhelper.sample.UrlImageViewHelperSample;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import com.parse.ParseUser;
public class Bla extends Activity
{
ImageView iv,bm;
TextView recipeTitle;
String urlForImage = "";
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bla_layout);
new urlTask("grilled mexican chicken").execute("grilled mexican chicken");
//new DownloadImageTask((ImageView)findViewById(R.id.RecipeImage)).execute(urlForImage);
}
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap>
{
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls)
{
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try
{
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
in.close();
}
catch (Exception e)
{
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result)
{
bmImage.setImageBitmap(result);
}
}
public class urlTask extends AsyncTask<String, Void, String>
{
String str;
public urlTask(String str)
{
this.str = str;
}
String tmpResult = str;
Document doc;
protected String doInBackground(String... urls)
{
String urldisplay = urls[0];
String url = "https://www.google.co.il/search?q=grilled+mexican+chicken&es_sm=93&source=lnms&tbm=isch&sa=X&ei=h1OqVOH6B5bjaqGogvAP&ved=0CAgQ_AUoAQ&biw=1920&bih=955";
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24); // Chrome not working
HtmlPage page = null;
try
{
page = webClient.getPage(url);
} catch (FailingHttpStatusCodeException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (MalformedURLException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
try
{
Document doc = Jsoup.parse(page.asXml());
Elements divs = doc.select(".rg_di");
for(Element div : divs)
{
Element img = div.select("a").get(0);
String link = img.attr("href");
System.out.println(link);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return tmpResult;
}
protected void onPostExecute(String result)
{
result = tmpResult;
urlForImage = tmpResult;
}
}
}
thanks for any help