I'm writing an Android app which does exactly the same as our iPad app for our company. But I have 1 issue while developing on the android. The app downloads a file from a webserver. It will call an URL like:
https://www.somedomain.com/API/Download.aspx?param1=test¶m2=test2 etc...
On the iPad this is working perfectly (I use the ASIHTTPRequest class for this). But on Android it is giving me only problems.
As soon as I want to download the file with the android, it downloads a file with a 500 internal server error HTML document instead of the PDF file.
I've checked the URL's, they look exactly the same as on the iPad.
The only thing I can imagine, is that the file which the user downloads is created "on the fly". So it takes some time (10 or 20 sec) to generate the file, and then the file is being downloaded.
On android I do this:
I have a class which extends:
extends AsyncTask<String, Integer, JSONObject>
In a method, I do this:
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
InputStream data = response.getEntity().getContent();
File file = new File(context.getDir("docs", Context.MODE_PRIVATE), FileName);
OutputStream output = new FileOutputStream(file);
ByteStreams.copy(data, output);
Closeables.closeQuietly(output);
But this is giving me a 500 internal server error doc instead of the desired PDF file. What am I missing here? (Sorry, I just started developing for Android so I'm not an expert in this case ;-))
Thanks in advance!
The fact that your response status code from the server is the problem means that this should have nothing to do with the Android stuff and only to do with the request you're sending. I notice, though this might be due to intentional omission, that you're doing a POST request without adding any POST params. Should this be a GET? I notice that the endpoint is an ASPX path with GET params in the query string. Maybe your server is set up to only respond to GET and not POST. How is this being done in the iOS code? Is there no differentiation between GET and POST, or is this abstracted from you via the library you're using?
Ok it works now... Stupid thing... I constantly created a new HttpClient so the session was not stored among connections. That is why the server returned a 500 internal server error because the user was not known by the server...
Thanks everyone for your help though!
Related
I made a game with Unity3D which is working on my own android phone fine, but it seems not working on other phones. I don't have access to those phones, but this is what I was told.
More detail: My game is an android app and needs to send data to a server to be stored. The URL is something like this "https://example.com/register". The server is hosted on the google app engine. When a user attempts to send the data he gets an error back which is something like this "java.io.FileNotFoundException https://example.com/register". I checked the logs in my server, there is no record regarding a request from that user. I read somewhere that it's because of htaccess and WWW class and you can't call an URL with htaccess data in it! But there is no htaccess to that page in my server. The page simply receives the data and store them, no authentication needed.
Would be great if anyone has any other idea why some devices get this error and some don't.
string url = "https://my-app-name.appspot.com/register";
WWWForm form = new WWWForm();
form.AddField("playerEmail", playerEmail);
form.AddField("registrationId", registrationId);
form.AddField("playerName", playerName);
form.AddField("playerPass", playerPass);
WWW www = new WWW(url, form);
yield return www;
the server-side then use those provided data and return a simple string.
<?php
$player_name = $_POST['playerName'];
$player_pass = $_POST['playerPass'];
$player_email = $_POST['playerEmail'];
$gcm_registration_id = $_POST['registrationId'];
//store data in the Datastore.
echo "success";
?>
app.yaml
application: my-app-name
version: 1
runtime: php55
api_version: 1
threadsafe: false
handlers:
- url: /register
script: register.php
I also had this problem. The takeaway I had was
Java.io.filenotfound exception is bogus error. It's not anything to do with file io - it's the request failing at the server.
To find the actual error in unity - look at the www requestHeaders - this will give you back the response headers from the request from the server - mine was a 500, but it could be a 400+ type error (especially if you're using authentication).
You can do this by doing a
foreach(var header in request.responseHeaders)
....
If anyone wants I can post my diagnostic code to help out.
I'm writing an Android app that should get data from a certain web application. That web app is based on Servlets and JSP, and it's not mine; it's a public library's service. What is the most elegant way of getting this data?
I tried writing my own Servlet to handle requests and responses, but I can't get it to work. Servlet forwarding cannot be done, due to different contexts, and redirection doesn't work either, since it's a POST method... I mean, sure, I can write my own form that access the library's servlet easily enough, but the result is a jsp page.. Can I turn that page into a string or something? Somehow I don't think I can.. I'm stuck.
Can I do this in some other way? With php or whatever? Or maybe get that jsp page on my web server, and then somehow extract data from it (with jQuery maybe?) and send it to Android? I really don't want to display that jsp page in a browser to my users, I would like to take that data and create my own objects with it..
Just send a HTTP request programmatically. You can use Android's builtin HttpClient API for this. Or, a bit more low level, the Java's java.net.URLConnection (see also Using java.net.URLConnection to fire and handle HTTP requests). Both are capable of sending GET/POST requests and retrieving the response back as an InputStream, byte[] or String.
At most simplest, you can perform a GET as follows:
InputStream responseBody = new URL("http://example.com").openStream();
// ...
A POST is easier to be performed with HttpClient:
List<NameValuePair> params = new ArrayList<NameValuePair>(2);
params.add(new BasicNameValuePair("name1", "value1"));
params.add(new BasicNameValuePair("name2", "value2"));
HttpPost post = new HttpPost("http://example.com");
post.setEntity(new UrlEncodedFormEntity(params));
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(post);
InputStream responseBody = response.getEntity().getContent();
// ...
If you need to parse the response as HTML (I'd however wonder if that "public library service" (is it really public?) doesn't really offer XML or JSON services which are way much easier to parse), Jsoup may be a life saver as to traversing and manipulating HTML the jQuery way. It also supports sending POST requests by the way, only not as fine grained as with HttpClient.
I am developing a Helpdesk application, in this app I am able to read and reply for the tickets sent by the customer. Now I got a requirement, I have to upload a file also. I have a post url to reply for the ticket, I will use Namevaluepairs to attach message with the url..
nameValuepair("id",ticketId);(nameValuepair is the instance of BasicNameValuePair)
nameValuepair("subject",subject);
nameValuepair("reply",message);
If file is not there then it will be like the above code. If file is there I one more parameter comes for file. What I have to do is to attach a file and encode it and then include it into Namevaluepair...
nameValuepair("file",encodedfile);
How can I Upload the file to this app and encode it.
I got some resources to attach a file to the default android email client using email intent, that was not helpful for me.
Thanks in Advance
Uploading a file is more complicated than adding a name-value pair. You need to get the output stream of the request and write the file. You can find an example here.
Use the Http client by Apache and its POST method:
DefaultHttpClient httpclient = appContext.GetHttpClient();
HttpPost httppost = new HttpPost("your.url.com");
httppost.setEntity(new FileEntity(yourFile, PLAIN_TEXT_TYPE));
HttpResponse response = httpclient.execute(httppost);
InputStream responseStream = new ByteArrayInputStream(responseString.getBytes("UTF-8"));
I'm building an Android app that runs off of a rails server. At first, when I tried to post simple String data to the server, I ran into an InvalidAuthenticityToken issue, but realized that I can bypass the authentication by setting the content type to "json"
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(Constants.REST_HOST + "/add_comment");
post.addHeader("Content-Type", "application/json");
The next step was trying to get upload profile picture working. However, when I tried uploading a photo via a MultipartEntity post, setting the content type to "json" causes the following error
StandardError (Invalid JSON string):
but not setting the content type brings back the InvalidAuthenticityToken exception. What's the correct way to post an image to a rails server from a foreign Java client?
ActionController::InvalidAuthenticityToken
(ActionController::InvalidAuthenticityToken):
Based on Jesse's suggestion, I ended up using
protect_from_forgery :except => :upload_avatar_pic
to disable authenticity check, but only for a specific function, so checks for browser requests are still validated.
You can disable authenticity checking on API non-get calls from non-web clients. You can do this in a before filter
class ApiController < ApplicationController
skip_before_filter :verify_authenticity_token
def create
#or whatever
end
end
The problem solved by Jesse Wolgamott said, but the page show "You are being redirected" message when I submit the form (Update,create,show). In before that the page redirected correctly. I am using rails 2.3.8.how to resolve this?
I am new to Android and so I may be missing some very basic things here.
I am trying to host custom KML files on a server behind my firewall and display those KML files on the Android emulator.
I started by writing a simple app that uses an Intent to display the overlay and pointing the app at geo:0,0?q=http://code.google.com/apis/kml/documentation/KML_Samples.kml. This works in the emulator.
Next I downloaded that KML file and posted it on my web server (Apache 2.2 on Fedora). I added an AddType directive for the .kml extension and restarted HTTPD.
When I point my simple app's Intent to my internally hosted KML file I get the error "The community map could not be displayed because it contains errors."
I added some code to try and download the KML file independently of the KML so I could check the status line and the like:
final HttpClient client = new DefaultHttpClient();
final HttpGet get = new HttpGet("http://mycompany.com/data/KML_Samples.kml");
try {
final HttpResponse resp = client.execute(get);
android.util.Log.e("MapOverlays", resp.toString());
} catch (Throwable t) {
android.util.Log.e("MapOverlays", "Exception", t);
}
With a breakpoint on the first Log message line I can inspect the results:
statusline = "HTTP/1.1 200 OK"
Content-Type: application/vnd.google-earth.kml+xml
Here's the Intent I'm using:
final Intent intent = new Intent(
android.content.Intent.ACTION_VIEW,
Uri.parse("geo:0,0?q=http://mycompany.com/data/KML_Samples.kml"));
startActivity(intent);
So the two main questions are
What do I need to do to get KML loaded from a private server?
What tools are available (if any) to determine what is wrong with what I've done (something more informative than "The community map...")?
Thanks
These may be silly questions, but I need to know whether you have verified that the KML file in question is in fact valid KML.
Another problem that you may have is that the KML file you are downloading contains network links which are themselves not reachable from your Android device.