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 that requests an image:
https://developer.android.com/training/volley/request.html#request-image
Edit- Example of use:
First you add the . library jar to your project. Then you can make the request very simple: just create an instance of Stringrequest (for text), in the constructor 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 last only need to implement the system, in onResponse the return will be in the method parameter. Last only 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 onResponse’s String(String s) to make the Parsing of an XML, but it could be any String or even a Json.
For Requestqueue the Google Documentation recommends using a Singleton, but can be instantiated as follows:
RequestQueue queue = Volley.newRequestQueue(this);
To request an image in the 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);
Stringrequest was replaced by an Imagerequest and onResponse parameter with a Bitmap
But is that recommended? It’s a car accessory sales app. What would be better, like this or using the client/server connection?
– Carlos Ximendes
What I would recommend you to do is to save the images to a server, be it Apache, IIS, Wildfly or any other server you prefer. Save the image path in Sqlite database on Android, for example,
IP_SERVIDOR:8080/aplicacao/imagens_carros/corcel.jpg
to view the image of the car Corcel, consulting in the Sqlite database that URL and passing the URL of the image where it is writtenIMAGE_URL
of the example I gave you. Any doubt just call.– Giancarlo Abel Giulian
Thanks, Giancarlo... I’ll try that way!
– Carlos Ximendes
Giancarlo, would you indicate some video of someone using this process?
– Carlos Ximendes
Worst I don’t have, I can create a page and make a tutorial as soon as I have time, but it’s not hard to create. You download Apache and install it on the machine, create an application directory, and inside that directory the images folder where the pictures of the cars are. Ai on Android performs the URL path request of the image. Brought.
– Giancarlo Abel Giulian
Giancarlo, I have done a lot of research but not solved these doubts: How to save images in Apache or Engix; How to save the path in the Sqlite database; What is the importance of JSON in this case? I’m sorry, but even though I’ve studied this subject a lot, I find it very fragmented. Deciding to use Fresco lib and Engix server, but not understanding the server access process.
– Carlos Ximendes
I created a repository on Github and I will do the example showing how it works once I have time, this week. Ai edit my post with the example here (;
– Giancarlo Abel Giulian
Right... Valew!
– Carlos Ximendes
Friend, I changed the post, I put a link that leads to one of my directories on Github with a small example project I did using Android and an Apache server carrying an image of a lion. There are instructions on how to install and configure Apache that I believe was what you wanted, so you can build a cool web service for your project. You have a reference to help you with anything.
– Giancarlo Abel Giulian
I couldn’t look before, but I’ll take a look and anything I ask for your help. Right? Thank you.
– Carlos Ximendes
Can’t your project be downloaded for testing? I found it very difficult to understand from Github.
– Carlos Ximendes
Yes, but you have to configure the development environment, I put there how to install, very easy.
– Giancarlo Abel Giulian
I opened your project, but it didn’t work... your image doesn’t open.
– Carlos Ximendes
It works because I tested it here before giving you the link.
– Giancarlo Abel Giulian