Is there any way of getting headers from inputstream object in Android? - android

I want to get the header information from a webservice call. That service returns a image but need to also get the header information. For getting the image I simply pass inputstream to decodeStream method. How can one get the headers too from that?
Regards
Sunil

Look at the code that does HTTP requests. Perhaps it's something along the lines of
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
return is;
(error handling and other details omitted in this snippet). Look up HttpResponse documentation, its getFirstHeader method is your friend!

If you're using a URLConnection to get your InputStream then yes, the same was as you would do it in standard Java. URLConnection has a method called getHeaderFieldKey(int) and another called getHeaderFields().
Example here.
Android URLConnection docs here

Related

Decode bitmap from stream without Apache HTTP Client

Until recently, I decoded images from a web resource using the Apache HTTP Client using this code:
HttpGet httpRequest = new HttpGet(params[0].toURI());
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufferedEntity = new BufferedHttpEntity(entity);
return BitmapFactory.decodeStream(bufferedEntity.getContent());
This all worked perfectly fine.
Now with Android 6, Apache HTTP Client has been deprecated. Not to worry, I thought, just use java.net.HttpUrlConnection instead as recommended here:
http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client
The code I tried and that I found in other questions here is:
HttpURLConnection connection = (HttpURLConnection) params[0].openConnection();
// connection.setRequestProperty("User-Agent", "");
connection.setRequestMethod("GET");
// connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
inputStream.close();
return bitmap;
This doesn't work. bitmap will always be null for the same image resource that works with the old code.
Does anyone have any insight into this? Here are other questions I tried and why they didn't work:
Android: bitmapfactory.decodestream returns null (Answers use deprecated methods/classes)
Bitmap.decodeStream returns null on specific existing (And working) images (Same as above)
The problem was caused by a simple HTTP/HTTPS issue. The image resource was requested from a http:// address. The server is set up to issue a 307 (temporary redirect) to the matching https:// address.
Although the default of HttpURLConnection is to follow redirects, the code given in the question didn't work. FYI Picasso didn't load the image either.
Requesting the image via its https:// address solved the problem.
Use Volley or Picasso. That's the recommended approach.

Android HttpPut Has No Body, Despite Setting the Entity

I'm trying to PUT some XML to a server, but the gist of it is that no matter what I do, HttpPut simply won't put anything in the Http body. The server always comes back saying that the body is missing, and looking at it through Wireshark, nothing is there! Here's the code I'm using to set up and run the request:
HttpPut putRequest = new HttpPut(urlString]);
StringEntity stringEntity = new StringEntity(xmlString, HTTP.ISO_8859_1);
stringEntity.setContentType("text/xml");
putRequest.setEntity(stringEntity);
putRequest.addHeader("Host", formatUrlForHostHeader(broadsoftUrl));
putRequest.addHeader("Authorization", authorizationString);
putRequest.addHeader("Content-Type", "text/xml");
putRequest.addHeader("Accept", "text/xml");
response = httpClient.execute(putRequest);
I'm not sure what else to include here. I tried it on 4.2 and 4.0.3. This code is running in the doInBackground of an AsyncTask. The response code I get is a 409 Conflict, and the body is the server's application-specific message, telling me the body is missing. I confirmed that it's missing with Wireshark.
EDIT:
An interesting note is that I ran the same code standalone on my desktop, and it worked. So, is there something up with the Android versions of HttpClient, or the system? I tried a few different API levels, too, just to check.
Any thoughts?
Thanks!
Alright, so the solution was to just give up on HttpPut and all that, and use HttpURLConnection. Here's how we ended up doing it:
URL url = new URL(theUrl);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setRequestProperty("Host", formatUrlForHostHeader(broadsoftUrl));
httpCon.setRequestProperty("Authorization", authorizationString);
httpCon.setRequestProperty("Content-Type", "text/xml; charset=ISO_8859_1");
httpCon.setRequestProperty("Accept", "text/xml");
httpCon.setDoInput(true);
httpCon.setDoOutput(true);
httpCon.setRequestMethod("PUT");
OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream(), "ISO_8859_1");
out.write(xmlData);
out.close();
if(httpCon.getErrorStream() == null) {
return "";
} else {
return "ERROR";
}
We didn't need to get the response from our PUT request, but you check if it failed by seeing if the error stream exists. If you wanted to get the response, you would do something like this:
StringWriter writer = new StringWriter();
IOUtils.copy(httpCon.getInputStream(), writer, encoding);
String responseString = writer.toString();
Of course, you would have to include Apache's IOTools in your app.
409 Conflict is usually an Edit Conflict error, usually associated with wikis, but it could be any type of conflict with the request.
http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
What type of data are you trying to post and is it possible that the host has existing data that cannot be changed?

How to connect to a webservice using Soap in Android?

I'm using HttpGet method for retrieving data from a web service in my Android app. Following is the code I'm using right now.
String url = URLEditor.encode("http://"+Constants.strURL+"Orders.asmx/CheckWebConnection? TechCode="+username+"&TechPIN="+password);
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if(entity == null) return false;
is = entity.getContent();
But, HttpGet method doesn't respond properly now and I'm asked to change the method. Web service doesn't support HttpPost. How can I do the same operation using Soap? Do I need to download any library and attach to Eclipse? Please help me
Download ksoap2. You will find lots of help with it including in stackoverflow, starting with :
How to call a .NET Webservice from Android using KSOAP2?

How to use HttpClient (or other HTTP class) to retrieve JSON asynchronously

I've got the content from a HttpClient, but I'm not sure where to go next, in order to parse my JSON result. Also, how do I do this asynchronously so that I can display a wait dialog to the user with the option of cancelling the current request (not bothered about the UI example, how do I setup the HTTP class to be cancellable)?
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://mysite/test.json");
try
{
HttpResponse response = client.execute(request);
// What if I want to cancel now??
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
}
Android has a class specifically for this sort of thing: AsyncTask.
See the answer to this question: How to create Http Connection using AsyncTask class?
I'm not a Android programmer but I imagine you need to start a thread.
http://download.oracle.com/javase/tutorial/essential/concurrency/

Uploading images to a PHP server from Android

I need to upload an image to a remote PHP server which expects the following parameters in HTTPPOST:
*$_POST['title']*
*$_POST['caption']*
*$_FILES['fileatt']*
Most of the internet searches suggested either :
Download the following classes and trying MultiPartEntity to send the request:
apache-mime4j-0.5.jar
httpclient-4.0-beta2.jar
httpcore-4.0-beta3.jar
httpmime-4.0-beta2.jar
OR
Use URLconnection and handle multipart data myself.
Btw, I am keen on using HttpClient class rather than java.net(or is it android.net) classes. Eventually, I downloaded the Multipart classes from the Android source code and used them in my project instead.
Though this can be done by any of the above mentioned methods, I'd like to make sure if these are the only ways to achieve the said objective. I skimmed through the documentation and found a FileEntity class but I could not get it to work.
What is the correct way to get this done in an Android application?
Thanks.
The Android source seems to come with an internal multipart helper library. See this post. At a quick glance, it's better documented than plenty of public APIs (cough cough, SoundPool, cough cough), so it should be a pretty good place to start, or possibly fine to just use a drop-in solution.
Maybe this post on the official Android group helps. The guy is using mime4j.
Another helpful resource could be this example in the Pro Android book.
I do exactly that to interact with an image hosting server (implemented also in php) that expects parameters as if they were posted from an html page.
I build a List<NameValuePair> of my keys and values something like this:
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair(key1, value1));
params.add(new BasicNameValuePair(key2, value2));
and then I pass it to my http helper class that sets the HttpEntity property of my HttpPost request. Here's the method straight out of my helper class:
public static HttpResponse Post(String url, List<NameValuePair> params, Context context)
{
HttpResponse response = null;
try
{
HttpPost request = new HttpPost();
request.setURI(new URI(url));
if(params != null)
request.setEntity(new UrlEncodedFormEntity(params));
HttpClient client = ((ApplicationEx)context.getApplicationContext()).getHttpClient();
response = client.execute(request);
}
catch(Exception ex)
{
// log, etc
}
return response;
}

Categories

Resources