6

我在我的应用程序上连接到两台服务器(PROD 是 https,测试服务器是 http)。

关于 J2ME:我可以毫无问题地连接到这两个服务器。在 Android 上我无法连接到测试服务器。当连接是http时,如果我不使用setChunkedStreamingMode,我无法获得responseCode(StringIndexOutOfBoundsException);如果我使用setChunkedStreamingMode,响应代码是401。我该怎么办,我的错在哪里??

这是我的android代码,如果你想看J2me代码,我也可以添加。

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 代码:

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)
4

2 回答 2

2

如果您使用的是 2.3 之前的设备,HTTPUrlConnection 有已知问题

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

于 2011-10-17T14:13:58.747 回答
1

我解决了这个问题。我使用 IP 地址而不是链接。服务器是Sharepoint服务器所以,它试图直接连接到sharepoint服务器,所以服务器需要身份验证:)不要直接使用ip :)

于 2011-10-19T14:14:12.477 回答