Edit: The thing causing the error was a typo in the url variable declaration. The code provided works, given correct input. See my answer for details.
Original Question: I'm working on an application that regularly sends a GET request to a certain web server.
I have tried and verified the URL and the query in the browser and I get the information in XML format as expected.
The URL/query looks like this:
http://foo.bar.com:8080/bla/ha/get_stuff?param1=gargle¶m2=blurp
I'm trying to get the raw content to my device (Android tablet) and output it to the screen.
However, when calling getInputStream() on my URLConnection object, I get the following exception:
java.net.ProtocolException: Unexpected status line: SSH-2.0-OpenSSH_5.9p1 Debian-5ubuntu1.1
Calling connect() on the same object causes no exception (however other methods, such as getContent(), do).
getContentType() returns null.
I'm using AsyncTask to collect and display the data (displaying works fine).
In the code is also an Authenticator part, but removing it has no change on the exception thrown, so I don't think it's the issue.
Is this because the data is in XML format?
If so, how else should I access it?
Code
class GetPositionTask extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String url = "http://foo.bar.com/8080/bla/ha/get_stuff";
String charset = "UTF-8";
String param1 = "gargle";
String param2 = "blurp";
try {
String query = String.format("param1=%s¶m2=%s",
URLEncoder.encode(param1, charset),
URLEncoder.encode(param2, charset));
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("userName", "passWord".toCharArray());
}
});
URLConnection urlConnection = new URL(url + "?" + query).openConnection();
urlConnection.setRequestProperty("Accept-Charset", charset);
InputStream response = urlConnection.getInputStream(); //Commenting this out prevents exception
return "Made it through!"; // Never reaches this
}
catch (IOException e) {
e.printStackTrace();
return "Exception in GetPositionTask";
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(s);
}
}
Note: This is similar to a couple of other questions, however I wasn't able to solve my problem reading those.