0

我正在做我的项目……现在我必须完成的任务是

1)我已经从android应用程序发送请求到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();

    }
}

}

但我想知道...我如何才能在服务器端... 1) 我必须使用 .jsp 扩展名制作简单页面才能提前完成我的工作

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 回答