0

データベースに対してphpスクリプトを実行して、リモートWebサーバーに接続するときに機能するアプリに問題があります。ただし、同じアプリを自分のマシンで実行されているローカルWebサーバーにポイントすると、動作しません。

リモートWebサーバーに接続するために使用するコードは次のとおりです(認証が必要です)。

(すべてのネットワークコードはAsyncTaskクラス内で実行されます。)

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
StringBuilder authentication = new   
    StringBuilder().append("frankh").append(":").append("vriceI29");
result = Base64.encodeBytes(authentication.toString().getBytes());
httppost.setHeader("Authorization", "Basic " + result);
nameValuePairs.add(new BasicNameValuePair("date", date));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();

認証を使用しないローカルサーバーへの接続については、次の行をコメントアウトしています。

//StringBuilder authentication = new   
//    StringBuilder().append("frankh").append(":").append("vriceI29");
//result = Base64.encodeBytes(authentication.toString().getBytes());
//httppost.setHeader("Authorization", "Basic " + result);

ただし、ローカルWebサーバーへのURLの表現方法に応じて、2つの異なるエラーが発生します。

このURLを使用する場合: "http://localhost.shoppinglistapp/fetchlist.php"

このエラーが発生します:

Error in http connectionjava.net.UnknownHostException: localhost.shoppinglistapp

URLのhttp部分をスキップすると、次のエラーが発生します。

Error in http connectionjava.lang.IllegalStateException: Target host must not be null,
or set in parameters.

私はここで何が間違っているのですか?リモートサーバーはLinuxApacheサーバーであり、ローカルサーバーはIIS 7です。ローカルサーバーは、インターネット接続がないか悪いときに作業するためだけのものであるため、重要ではありませんが、わからないのは嫌です。なぜ物事がうまくいかないのか。

4

1 に答える 1

0

ローカルエミュレータを介してテストする場合は、「localhost」の代わりに10.0.2.2を使用することをお勧めします。

エミュレートされた環境からローカルホストを参照する

于 2012-09-18T21:33:06.693 に答える