0

我编写了一个逻辑,它调用一个传递几个参数的 android web 服务。问题是当我发送查询时,它返回一条错误消息,我将其作为 xml 获取。我要调用的网址是 http://192.168.1.10:8080/ymaws/resources/restaurantcityid=33498&areanm=vasant vihar 但我收到错误。代码如下。请提出一个很好的方法来做到这一点

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());
4

2 回答 2

0

这可能需要您使用 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 Client jar

于 2012-02-17T08:37:02.137 回答
0

尝试简单地添加“amp;” 在“&”符号之后。(不能一起写 & + amp; 因为 SO 只写了“&”呵呵):)

于 2012-02-17T09:34:44.547 回答