【问题标题】:Request send from Android Mobile is get no any response on server从 Android Mobile 发送的请求在服务器上没有得到任何响应
【发布时间】:2013-01-07 10:49:27
【问题描述】:

我是 android 世界和移动应用程序服务器端编程的新手。我遇到了一个问题:- 当从任何 android 手机向我发送 url 形式的请求时,我的服务器(Apache tomcat)没有找到关于该请求的任何响应。

我正在客户端(Android Mobile)编写以下代码:-

public static void requestForDepartment()
{
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    String iName = null;
    String iprice = null;
    String idPrice = null;
    nameValuePairs.add(new BasicNameValuePair("storeId","s0001"));

    try {
        System.out.println("inside server try 1");
        // start - line is for sever connection/communication
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new
          HttpPost("http://192.168.1.3:8080/xybuy/rest/webservices/requestDepatment");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        // end - line is for sever connection/communication
        InputStream is = entity.getContent();
    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection "
                + e.toString());
    }
}

192.168.1.3:8080 是我的端点。这是我在服务器端的 web.xml 文件

        <?xml version="1.0" encoding="UTF-8"?>
        <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"            xmlns="http://java.sun.com/xml/ns/javaee"    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"    version="2.5">
  <display-name>FirstProject</display-name>
  <welcome-file-list>
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>index2.jsp</welcome-file>
  </welcome-file-list>
 <servlet>
 <servlet-name>ServletAdaptor</servlet-name>
 <servlet-class>
        com.sun.jersey.server.impl.container.servlet.ServletAdaptor</servlet-  class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/REST/*</url-pattern>
</servlet-mapping>

实际负载可以是 JSON 或 XML。并且服务器是restful webservices。

【问题讨论】:

  • REST != 休息,这可能是原因吗?
  • 我不认为 REST 是原因......
  • 怎么知道是json还是xml? (假设您没有在请求标头中指定它?)

标签: android web-services rest httprequest httpresponse


【解决方案1】:

1) 尝试在您的请求中添加标头,比如

httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");

java 默认会添加一些头文件,但有时这还不够。

2) 如果您从本地 PC 在 java 项目中运行此代码,它是否有效?

【讨论】:

  • 服务器端编码工作正常。我正在使用 RESTfulwebservices。响应是 json 对象
  • 什么意思是“我的服务器(Apache tomcat)没有找到关于该请求的任何响应”。你的意思是webserver没有找到对应“storeId”、“s0001”的数据?
【解决方案2】:

@Eugene 是对的,但您的问题完全不清楚。你说它的xml和json。怎么可能 ?如果是 xml,那么你需要一个“application/xml”作为内容类型,如果它是 json,那么你的 content-type = “application/json”。

【讨论】:

    猜你喜欢
    • 2021-03-20
    • 2016-12-26
    • 2013-11-01
    • 1970-01-01
    • 2018-01-21
    • 2019-09-02
    • 1970-01-01
    • 2014-01-12
    • 2013-05-13
    相关资源
    最近更新 更多