What is the difference between httpconnection on J2ME and HttpUrlConnection on Android (http error 401)
Asked Answered
Q

2

6

I connect to two servers (PROD is https, test server is http) on my applicaitons.

on J2ME: I can connect to this two servers without a problem. on Android I can't connect to test-server. When connection is http, if I dont use setChunkedStreamingMode, I cant get responseCode(StringIndexOutOfBoundsException); if I use setChunkedStreamingMode, response code is 401. What should I do, where is my fault??

Here is my android code, Also if you want to see J2me code, I can add it, too.

URL url = new URL(getUrl());
            URLConnection conn = url.openConnection();
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setConnectTimeout(10000);
            httpConn.setRequestProperty("User-Agent", util.getDeviceFullModel()
                    + " " + util.getSoftwareVersion());
            httpConn.setRequestProperty("Accept-Charset", "utf-8");

            httpConn.setRequestProperty("Content-Type",
                    "text/xml; charset=utf-8");
            httpConn.setRequestProperty("SOAPAction",
                    "http://tempuri.org/IAuthenticationServiceForGroup/"+conTypeString);
            httpConn.setRequestProperty("Software-Version", AppData.VERSION);
            httpConn.setChunkedStreamingMode(getParams().getBytes("UTF8").length);
            httpConn.setRequestMethod("POST");
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);
            httpConn.connect();

            os = httpConn.getOutputStream();
            os.write(getParams().getBytes("UTF8"));

            try {
                os.close();
            } catch (Exception e) {
                onError(e);
            }
            response=httpConn.getResponseCode();

J2ME code:

HttpConnection c = (HttpConnection)XConnection.openConnection(XConnection.SERVER + "AuthenticationServiceForGroup.svc");

            c.setRequestProperty("User-Agent", XUtil.getDeviceFullModel() + " " + XUtil.getSoftwareVersion());
            c.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
            c.setRequestProperty("SOAPAction", "http://tempuri.org/IAuthenticationServiceForGroup/"+conType);
            c.setRequestProperty("Software-Version", XApp.VERSION);
            c.setRequestMethod(HttpConnection.POST);

            OutputStream os = null;

            os = c.openOutputStream();
            os.write(sParams.getBytes());

            try {os.close();} catch (Exception e) {}

            if (c.getResponseCode() == HttpConnection.HTTP_OK)
Quenelle answered 14/10, 2011 at 9:17 Comment(1)
could you please add your j2me code too?Ruffina
Q
1

I solved this problem. I use ip adress instead of link. Server was Sharepoint server so, It tries to connect to directly sharepoint server, so server wants Authentication:) Dont use directly ip:)

Quenelle answered 19/10, 2011 at 14:14 Comment(0)
H
2

If you're using pre-2.3 devices, HTTPUrlConnection has known issues

http://android-developers.blogspot.com/2011/09/androids-http-clients.html

Hughie answered 17/10, 2011 at 14:13 Comment(0)
Q
1

I solved this problem. I use ip adress instead of link. Server was Sharepoint server so, It tries to connect to directly sharepoint server, so server wants Authentication:) Dont use directly ip:)

Quenelle answered 19/10, 2011 at 14:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.