2012-02-17 8 views
0

私は、アンドロイドウェブサービスを呼び出すロジックを書いて、いくつかのパラメータを渡しました。問題は、私がXMLとして取得するエラーメッセージを返すクエリを送信するときです。私が呼びたいURLは 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

は「アンプを追加するだけで試してみてくださいApacheのHTTPクライアントjarファイルを取得することができます; " 「&」の記号の後に入力します。 (& + ampを一緒に書くことができませんでしたので、「&」となりました:)

+0

私はこのように書いて、再び強制終了しました。私はもう一度それをやっていると思う。 samir

+0

友達が解決策を得ました。実際の問題は、スペースを含む最後のパラメータを渡していたことでした。それはエラーを投げていたので、私はそれらのスペースを削除する方法を検討する必要があります。%2​​0は方法ですが、 – samir

0

を行うための良い方法を提案これはApacheのオープンソースのLIBSを使用するには、uを必要とするかもしれないが、それはすべて私のコードで私の作品:

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; 
} 

必要なlibsのは、次のとおりです。

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; 

あなたはhere

+0

「http://192.168.1.10:8080/ymaws」のようなURLを呼び出すとそのまま動作します/ resources/restaurant/"+ cityid +"/cuisineines/"これは都市の賢明な料理のxmlを返します。しかし、私はそれがそれと関係していると思います。 " URLに渡すことにサインしてください – samir

+0

どのようにバックエンドをやっているのかわかりませんが、url paramsを渡すと何が起こっているのでしょうか?urlのスキームを次のように変更してください: '/ ymaws/resources/restaurant/city_id/area_nm/'?だから、あなたはそれらをハンドラのパラメータとして選ぶだけです。少なくともDjangoはこれを非常に簡単に提供しています:-) – nemesisfixx

+0

友人は解決策を得ました。実際に私は問題を理解できませんでした。実際の問題は、スペースを含む最後のパラメータを渡していることでした。それは間違いを投げていた。今私はそれらのスペースを削除する方法に取り組む必要があります。私は20%の方法しか知っていませんでしたが、いくつかのロジックを実装する必要があります – samir

関連する問題