调用 Restfull Web 服务在 URI 中抛出错误

发布于 2025-01-06 21:09:22 字数 2919 浏览 1 评论 0原文

我编写了一个调用 android webservice 并传递几个参数的逻辑。问题是当我发送查询时,它返回一条错误消息,我得到的是 xml 格式的错误消息。我想调用的网址是 http://192.168.1.10:8080/ymaws/resources/restaurantcityid=33498& ;areanm=vasant 维哈尔 但我收到错误。代码如下。请建议一个好方法来做到这一点

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

I have written a logic that calls a android webservice passing few paramters. The problem is when i send query it returns an error message which i m getting as an xml. the url that i want to call is
http://192.168.1.10:8080/ymaws/resources/restaurantcityid=33498&areanm=vasant vihar
but i m getting error.The code is below. Plz suggest a good way to do this

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

过度放纵 2025-01-13 21:09:22

尝试简单地添加“amp;”在“&”之后符号。 (无法将 & + amp; 写在一起,因为 SO 仅将其写为“&”呵呵):)

Try simply adding "amp;" after the "&" sign. (couldn't write & + amp; together since SO made it to only "&" hehe) :)

你曾走过我的故事 2025-01-13 21:09:22

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

This might require u to use Apache's opensource libs, but it works for me in all my code:

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

The required libs are :

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;

You can get the Apache Http Client jars here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文