-1

我是 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。服务器是宁静的网络服务。

4

2 回答 2

0

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

于 2013-01-07T12:42:51.320 回答
0

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

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

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

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

于 2013-01-07T11:17:43.157 回答