When I need to access some web content with android I use the Volley library, it is very simple to use. Here is a link to an example requesting an image:
link
Edit - Usage example:
First you add the .jar library to your project. Then you can perform the request very simply: just create an instance of StringRequest (for text), in the counter you can explain the method you need (POST or GET), then the URL (in my example the url is in RequestContract .XMLQuestionarioContract.XML_QUESITIONARIO_URL) and lastly only need to implement the listener, in onResponse the return will be in the method parameter. Finally you just need to add the request in a queue.
private void requestQuestionario(){
String tag_string_req = "req_questionario"; //tag para cancelar o request
pDialog.setMessage("Carregando Questionário...");
showDialog();
StringRequest stringRequest = new StringRequest(Request.Method.GET,
RequestContract.XMLQuestionarioContract.XML_QUESITIONARIO_URL, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
Log.d(TAG, "Response = " + s);
//TODO -- Tratar Response
InputStream xml = new ByteArrayInputStream(s.getBytes());
try {
HashMap questionario = XmlParser.parse(xml);
List<Questao> questoes = (List) questionario.get("questoes");
List<Alternativa> alternativas = (List) questionario.get("alternativas");
List<Codigo> codigos = (List) questionario.get("codigos");
DataBaseHelper db = new DataBaseHelper(getApplicationContext());
db.insertQuestoes(questoes);
db.insertAlternativas(alternativas);
db.insertCodigos(codigos);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e){
e.printStackTrace();
}
hideDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
//TODO -- Tratar erro
Log.e(TAG, "Erro no request");
hideDialog();
}
});
requestQueue = RequestManager.getInstance(this).getRequestQueue();
stringRequest.setTag(tag_string_req);
requestQueue.add(stringRequest);
}
In this example I use the contents of the onResponse's String (s) to parse an XML, but it could be any String or even a Json.
For RequestQueue, Google Documentation recommends using a Singleton, but can be instantiated as follows:
RequestQueue queue = Volley.newRequestQueue(this);
To request an image in google documentation, use this example:
ImageView mImageView;
String url = "http://i.imgur.com/7spzG.png";
mImageView = (ImageView) findViewById(R.id.myImage);
...
// Retrieves an image specified by the URL, displays it in the UI.
ImageRequest request = new ImageRequest(url,
new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
mImageView.setImageBitmap(bitmap);
}
}, 0, 0, null,
new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
mImageView.setImageResource(R.drawable.image_load_error);
}
});
// Access the RequestQueue through your singleton class.
MySingleton.getInstance(this).addToRequestQueue(request);
The StringRequest has been replaced by an ImageRequest and the onResponse parameter by a BitMap