6

アプリケーションで 2 つのサーバー (PROD は https、テスト サーバーは http) に接続します。

J2ME の場合: この 2 つのサーバーに問題なく接続できます。Android では、テスト サーバーに接続できません。接続が http の場合、を使用しないと、 を使用するとsetChunkedStreamingMode取得できません。応答コードはです。どうしよう、どこが悪いの??responseCode(StringIndexOutOfBoundsException);setChunkedStreamingMode401

これが私の 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 に答える