调用restfull webservice抛出错误的URI

问题描述:

我写了一个逻辑,调用一个android webservice传递几个参数。问题是当我发送查询它返回一个错误消息,我得到一个XML。我想要打电话的网址是 http://192.168.1.10:8080/ymaws/resources/restaurantcityid=33498&areanm=vasant vihar 但我得到错误。代码如下。 PLZ建议一个好办法做到这一点调用restfull webservice抛出错误的URI

String list = null; 
       restaurantnames=new ArrayList<String>(); 
       areanames=new ArrayList<String>(); 
       restaurantidlist=new ArrayList<String>(); 

       final HttpClient client=new DefaultHttpClient(); 
            String url = "http://192.168.1.10:8080/ymaws/resources/restaurant?cityid="+cityid+"&areanm="+area.getSelectedItem().toString(); 
       String encodedurl = null; 
       try 
       { 
         encodedurl = URLEncoder.encode(url,"UTF-8"); 
       } 
       catch (UnsupportedEncodingException e1) 
       { 
         e1.printStackTrace(); 
       } 
       Log.i("TEST", encodedurl); 

       final HttpGet req=new HttpGet(encodedurl); 
       HttpResponse httpResponse; 
       try { 
         httpResponse=client.execute(req); 
         HttpEntity entity = httpResponse.getEntity(); 
         Log.i("entity", entity.toString()); 
         if (entity != null) 
         { 
          InputStream instream = entity.getContent(); 
          BufferedReader reader = new BufferedReader(new InputStreamReader(instream)); 
          StringBuilder sb = new StringBuilder(); 

          String line = null; 
          try 
          { 
           while ((line = reader.readLine()) != null) 
           { 
            sb.append(line + "\n"); 
           } 
          } 
          catch (IOException e) 
          { 
           e.printStackTrace(); 
          } 
          finally 
          { 
           try 
           { 
            instream.close(); 
           } 
           catch (IOException e) 
           { 
            e.printStackTrace(); 
           } 
          } 

          // Closing the input stream will trigger connection release 
          list= sb.toString(); 
          Log.i("list xml is", list.toString()); 

尽量简单地增加“放;”之后的“&”标志。 (写不出& +功放;既然在一起,因此它作出唯一的“&”嘿嘿):)

+0

我这样写了它,它又被迫关闭。我想我会再次犯错。 请求应该形成为任何进一步的建议?感谢您的帮助亲爱的BTW – samir 2012-02-17 10:59:52

+0

朋友得到了解决方案。其实我couldnt理解这个问题,真正的问题是我传递了包含空格的最后一个参数,这就是抛出错误,现在我需要去解决这些空间,我写了%20是方法,但需要实现一些逻辑 – samir 2012-02-17 11:59:39

这可能需要u到使用Apache的开源库,但它在我所有的代码工作对我来说:

public static String getHTTP(String url) throws ClientProtocolException, IOException { 
     String result = "";  
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpGet httpget = new HttpGet(url); 
     ResponseHandler<String> responseHandler = new BasicResponseHandler();   

     // Execute HTTP Get Request 
     result = httpclient.execute(httpget, responseHandler); 
     return result; 
} 

所需要的库有:

import org.apache.http.client.HttpClient; 
import org.apache.http.client.ResponseHandler; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.ResponseHandler; 
import org.apache.http.client.ClientProtocolException; 
import java.io.IOException; 

你可以得到Apache的HTTP客户端罐子here

+0

它的工作,因为它是如果我们调用一个网址,如“http://192.168.1.10:8080/ymaws/resources/restaurant /“+ cityid +”/ cuisines /“返回城市明智的菜肴的xml。但我认为这与这个“有关”?签署,即时通过url – samir 2012-02-17 08:50:22

+0

我不知道你如何做后端,但如果传递在URL参数是什么搞砸了这个,你怎么改变URL方案如下:'/ ymaws/resources/restaurant/city_id/area_nm /'?所以你只需把它们作为处理程序的参数。至少Django提供这个很容易:-) – nemesisfixx 2012-02-17 11:23:50

+0

朋友得到了解决方案。其实我无法理解这个问题。真正的问题是我传递了包含空格的最后一个参数。这就是抛出错误。现在我需要研究如何去除这些空间。我编写%20是方法,但需要实现一些逻辑 – samir 2012-02-17 11:59:32