android - 在服务器端 jsp 接受来自 android 的请求

标签 android jquery jsp tomcat

我正在做我的项目......现在我必须完成的是

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"

最佳答案

使用android模拟器,你应该定义服务器ip地址为10.0.2.2

参见-> http://developer.android.com/tools/devices/emulator.html (网络地址空间)

使用真实的 android 设备,通过有线网络或无线(如果 android 设备和服务器连接到同一网络),您应该通过 ip 或名称(如果 dns 服务提供它)进行连接。

如果设备和服务器位于不同的网络上,您可能会遇到其他问题,例如路由或防火墙问题。

关于android - 在服务器端 jsp 接受来自 android 的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13433925/

相关文章:

android - Activity 生命周期 - startActivity() 和 extras

android - IntelliJ : project not generated with android tool - permission denied

javascript - 用于编辑页面信息的 jquery 启动屏幕

java - 如何将 spring @ModelAttribute 与构建器模式一起使用

java - 得到 ® 作为?从 Struts 2 JSP 页面读取时

android - ProgressDialog 旋转圈

java - 由 : android. support.v4.app.Fragment$InstantiationException 引起:无法实例化 fragment

javascript - 如何以编程方式从 Chrome 扩展内容脚本中选择 select2 中的选项?

javascript - jquery 表中的搜索和条目计数

java - 使用 JDBC 和 JSP 的 UPSERT MYSQL 命令