0

私は自分のプロジェクトに取り組んでいます...今私が達成しなければならないことはそれです

1)私はアンドロイドアプリケーションからTomcatサーバー上のページにリクエストを送信しました

2)ページはアプリケーションへのリクエストとレスポンスを取得します

クライアント側では、このコードを使用しています

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Creating HTTP client
    HttpClient httpClient = new DefaultHttpClient();
    // Creating HTTP Post
    HttpPost httpPost = new HttpPost(
            "http://www.example.com/login");

    // Building post parameters
    // key and value pair
    List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
    nameValuePair.add(new BasicNameValuePair("email", "user@gmail.com"));
    nameValuePair.add(new BasicNameValuePair("message",
            "Hi, trying Android HTTP post!"));

    // Url Encoding the POST parameters
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
    } catch (UnsupportedEncodingException e) {
        // writing error to Log
        e.printStackTrace();
    }

    // Making HTTP Request
    try {
        HttpResponse response = httpClient.execute(httpPost);

        // writing response to log
        Log.d("Http Response:", response.toString());
    } catch (ClientProtocolException e) {
        // writing exception to log
        e.printStackTrace();
    } catch (IOException e) {
        // writing exception to log
        e.printStackTrace();

    }
}

}

しかし、私は知りたい...どうすればサーバー側にドンすることができますか ...

2)テストのために少しガイドしてください

 a)do i have to switch on wifi of laptop...

 b)and also wifi of my android phone

 c)connect phone through wifi with laptop by IP of laptop

 d)then after connecting throught ip hit localhost server by..."192.168.1.23/file.jsp"
4

1 に答える 1

0

Androidエミュレーターを使用して、サーバーのIPアドレスを10.0.2.2として定義する必要があります

参照-> http://developer.android.com/tools/devices/emulator.html (ネットワーク アドレス空間)

ケーブルネットワーク、またはAndroidデバイスとサーバーが同じネットワークに接続されている場合はワイヤレスのいずれかで、実際のAndroidデバイスを使用すると、IPまたはDNSサービスが提供している場合は名前による接続が必要です。

デバイスとサーバーが異なるネットワーク上にある場合、ルーティングやファイアウォールの問題など、他の問題が発生する可能性があります。

于 2013-03-18T15:12:34.450 に答える