2013-01-07 23 views
-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是我的終點。這是在服務器端

 <?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我的web.xml文件。而服務器是寧靜的web服務。

+0

REST!=休息,可能是原因嗎? – njzk2

+0

我不認爲REST是原因...... – user206516

+0

你怎麼知道它是json還是xml? (鑑於您沒有在請求頭中指定它) – njzk2

回答

0

1)嘗試添加標題您的要求,說

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

的Java默認情況下增加了一些頭,但有時這是不夠的。

2)如果你在本地PC的Java項目中運行此代碼,它會工作嗎?

+0

服務器端編碼正在工作。我正在使用RESTfulweb服務。響應是json對象 – user206516

+0

什麼意思是「我的服務器(Apache tomcat)沒有找到任何有關該請求的響應」。你的意思是說該網絡服務器沒有找到與「storeId」,「s0001」相對應的數據? –

0

@Eugene是對的,但你的問題完全不清楚。你說它的XML和JSON。怎麼可能 ?如果它的xml,那麼你需要一個「application/xml」作爲content-type,如果它的json那麼你的content-type =「application/json」。

相關問題