我是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
1)尝试在请求中添加标题,例如
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
java默认添加一些头文件,但有时这还不够。
2)如果您在本地PC上运行java项目中的代码,它是否工作?
@Eugene是对的,但是你的问题完全不清楚。你说它是xml和json。这怎么可能呢?如果是xml,那么你需要一个"application/xml"作为内容类型,如果是json那么你的content-type = "application/json"