i try to build an https client for android and i need do get some request of an Servlet
but when i use the getInputStream method the applicaion hangs.
There is no error only hanging when i call the method.
String url = "https://.../Servlet";
try {
mPushLiteConnection = (HttpsURLConnection) new URL(url).openConnection();
mPushLiteConnection.setDoOutput(true);
mPushLiteConnection.setDoInput(true);
mPushLiteConnection.setRequestMethod("POST");
mPushLiteConnection.connect();
subscribe();
InputStream in = (InputStream)mPushLiteConnection.getInputStream();
unsubscribe();
mPushLiteConnection.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this is only to check the call, but it didtn work.
Any idea why it hangs and tell me nothing?
the subscribe method works, when i comment out the line with the inputstream, the server show me all is correct.
I can try the same with the Firefox and it works and i can see the request.
i also put the keystore into the TrustManager.
sorry for my english i hope i explain it ennougth.
THX
I think i know the reason!
the method getInputStream() needs (why ever) the contentLength!
In the servlet i want to connect to is no contentLength couse it is an push chunked servlet... it sends all the time pieces of XML without length.
So.. HttpsURLConnection are not usefull for chunked!
Now i try the same connection with httpClient
i hope it works for it... if not: i jump behind the bus ;)
i use a different Thread
and yes i installed the certificate on the client side
i have a second connection to load a list from another servlet and it works fine, but i dont need a InputStream for it only a SAX Parser.
But i cant use the SAX Parser for this servlet, couse i dont get an XML, only XML Tags without the Start Document Tag
maybie you know how to ignore the Start Document tag? I am not sure if i need it for SAX
Related
here is url ...
https://api.thingspeak.com/update?api_key=R3LQP7IXIXEQ1OIV&field1=0
Actually..I don't want to get the content of url.
I just want to update the value of field1 to 1 and 0.
If I just type this url in any broswer,my thingspeak data will be update.All I need to do is to write code like typing in any broswer of this url.
In that way I think I can turn on and off my led through android application for my IOT project.I think all I need to do is to make connection between apk and thingspeak from this url.I am new to android studio.I have tried many ways.Help me please.
Thanks a lot.
You can simply use a HTTP GET method (that is what your browser does).
To answer your question:
try {
// create the HttpURLConnection
url = new URL(yourUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// just want to do an HTTP GET here like your browser would
connection.setRequestMethod("GET");
// give it 15 seconds to respond
connection.setReadTimeout(15*1000);
connection.connect();
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
connection.disconnect();
}
There are better (cleaner) ways to do his, but this should answers your question.
PS: Be aware that in Android you will not be able to do this on the main thread! You should do this in a AsyncTask, AsyncTaskLoader, ...
How can I receive Pushbullet notes/images/profile details via API-Key on Android? My main problem is the SSL security in that point. (BTW: I'm pretty much a beginner in Android and only know the basics.)
The auth looks like this:
https://apikey#api.pushbullet.com/v2/users/me
I'm successfully requesting the content of a webpage (e.g. wikipedia.org) via
try {
URL url = new URL("https://www.wikipedia.org");
URLConnection urlConnection = url.openConnection();
InputStream in = urlConnection.getInputStream();
StringWriter writer = new StringWriter();
IOUtils.copy(in, writer);
theString = writer.toString();
textView.setText(theString);
} catch (Exception e) {
textView.setText("Error: "+e "String content: " +theString);
}
but when I'm requesting, for example, my profile details I'm always getting a
javaio FileNotFoundException
If you run your requests through https://www.runscope.com you can often see what the request is that your client is actually making (they have a free plan for trying it out).
If I had to guess I would say it's likely the authorization is not working correctly. Are you able to get that page using curl? It should look something like:
curl -u <apikey>: https://api.pushbullet.com/v2/users/me
Assuming that works, try something like
urlConnection.setRequestProperty("Authorization", "Bearer <apikey>");
or however it is you set headers on your HTTP requests. This is more explicit than relying on the https://password#domain url thing.
I am trying to test downloading and storing an image (any image) using Robolectric but I keep getting a 403. The URL is properly accessible and work with Android's VM, also works when I open in the browser.
Here is what I am trying to test.
try{
URLConnection urlConnection = new URL("http://<any image url>").openConnection();
InputStream inputStream = urlConnection.getInputStream();
// call a method to write to disk
} catch(Exception ex){
ex.printStackTrace();
}
My Roboelectric method fails when I try to get the inputStream. It always returns a 403. I looked at the FalseHTTP thing as well but it only seems to work with apache's connection.
Is this a Roboelectric problem or what am I doing incorrectly ? Robolectric is properly configured. RUnnables work. It fails over here.
Thanks!
Try Robolectric.getFakeHttpLayer().interceptHttpRequests(false) to allow the requests to actually go through instead of being intercepted.
I'm working on an android app that has to send and receive information from a 3rd party server. I'm not experienced at this at all, so bear with me if I give too much/too little/not the right kind of information at first.
The API that was provided to send information has the format
https://methodurl.com/username=user&password=pass&key=key&info=infotosend
When I put this URL into my desktops browser, it returns a string, which I'm also trying to get. At least, I'm assuming all it returns is a string, since if I look at the page source, there is just a string, such as "200 OK" or "401 Unauthorized".
The code I'm using is what I can glean from the web what I'm supposed to use. Unfortunately, for all I know, I'm writing code to do something very different than what I want. I've never written code to interact with a server before, so this is all new to me.
Here's my code:
TextView urlView = (TextView)findViewById(R.id.url_view);
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(siteToSubmit);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
String serverResponse = client.execute(request, responseHandler);
urlView.setText(serverResponse);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Logcat in eclipse (when app is running on the emulator) is
java.net.UnknownHostException
The way I understand the code to be working is I'm creating a client that will execute on a url (like would happen when I hit enter in the URL bar on a browser), a request for that client to execute on, and a handler to receive the response from the server, which is set to be recieved as a string. I am then trying to set that string as the text in a TextView to verify that I am getting the proper response. Right now, I just get the log error, and urlView does not change. Any ideas why? Am I understanding the process correctly?
Thanks!
Add the uses-permission android.permission.INTERNET to the manifest file.
I have a text file on my server. I want to open the text file from my Android App and then display the text in a TextView. I cannot find any examples of how to do a basic connection to a server and feed the data into a String.
Any help you can provide would be appreciated.
Try the following:
try {
// Create a URL for the desired page
URL url = new URL("mysite.com/thefile.txt");
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
// str is one line of text; readLine() strips the newline character(s)
}
in.close();
} catch (MalformedURLException e) {
} catch (IOException e) {
}
(taken from Exampledepot: Getting text from URL)
Should work well on Android.
While URL.openStream will work, you would be better off using the Apache HttpClient library that comes with Android for HTTP. Among other reasons, you can use content encoding (gzip) with it, and that will make text file transfers much smaller (better battery life, less net usage) and faster.
There are various ways to use HttpClient, and several helpers exist to wrap things and make it easier. See this post for more details on that: Android project using httpclient --> http.client (apache), post/get method (and note the HttpHelper I included there does use gzip, though not all do).
Also, regardless of what method you use to retrieve the data over HTTP, you'll want to use AysncTask (or Handler) to make sure not to block the UI thread while making the network call.
And note that you should pretty much NEVER just use URL.openStream (without setting some configuration, like timeouts), though many examples show that, because it will block indefinitely if you the server is unavailable (by default, it has no timeout): URL.openStream() Might Leave You Hanging.
Don't forget to add internet permissions to the manifest when taking net resources: (add in manifest).