1

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&param2=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&param2=%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.

2 Answers 2

1

The cause was a simple type in the url string, which was supposed to be

String url = "http://foo.bar.com:8080/bla/ha/get_stuff";

rather than

String url = "http://foo.bar.com/8080/bla/ha/get_stuff";

Fixing that typo made the whole thing work as a charm.

Interestingly though, when I circumvented the whole concatenation and formatting business by pasting the complete URL/query into the URLconstructor as below, it still worked (even though the URLEncoder#encode calls did switch some :characters out in param2).

URLConnection urlConnection = new URL("http://foo.bar.com:8080/bla/ha/get_stuff?param1=gurgle&param2=blurp").openConnection();

(in my real case, the param2 variable includes a MAC address, and the :'s were replaced with something of the type %xx)

Sign up to request clarification or add additional context in comments.

Comments

0

You're connecting to an SSH server, not an HTTP server.

The reason it doesn't happen on connect() is that connect() doesn't actually connect. The other methods you mention do, if necessary.

2 Comments

Funny that the server owner just specified "The above XMP is the response to a HTTP GET request to [link]". So I'm connecting to an SSH server through a HTTP request?
Of course I meant XML, not XMP. Anyway, if you could point me in the right direction here, I'd be very grateful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.