Android 中的 Web 服务问题

发布于 2024-11-02 19:00:40 字数 238 浏览 1 评论 0原文

我正在使用代码调用 json webservice

HttpClient client = new DefaultHttpClient();
  HttpGet method = new HttpGet(Url);
  HttpResponse response = client.execute(method);

执行最后一行后,应用程序总是捕获异常。

任何人都可以帮助我吗?

I am calling a json webservice using the code

HttpClient client = new DefaultHttpClient();
  HttpGet method = new HttpGet(Url);
  HttpResponse response = client.execute(method);

After executing the last line,app always catching the exception.

Could any one help me in this??

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

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

发布评论

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

评论(2

感性 2024-11-09 19:00:40
  try this

HttpPost httppost;
        HttpClient httpclient;
        List<NameValuePair> nameValuePairs;

        String userInfo[] = getUserInfo();
        String uname = userInfo[0];
        String pass = userInfo[1];

        if(uname != null && pass != null) {
            String bytesSent;
            httppost = new HttpPost(getURL());  
            httpclient = new DefaultHttpClient();
            nameValuePairs = new ArrayList<NameValuePair>(2);  
            String reminderstatus = "P";

            nameValuePairs.add(new BasicNameValuePair("doAction", "something"));
            nameValuePairs.add(new BasicNameValuePair("username",uname ));
            nameValuePairs.add(new BasicNameValuePair("password", pass));


            try {        
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  
                HttpResponse response = httpclient.execute(httppost);
                InputStream is = response.getEntity().getContent();
                BufferedInputStream bis = new BufferedInputStream(is);
                ByteArrayBuffer baf = new ByteArrayBuffer(20);
                int current = 0;  

                while((current = bis.read()) != -1) {  
                    baf.append((byte)current);  
                }  

                bytesSent = new String(baf.toByteArray());
            }
        }
  try this

HttpPost httppost;
        HttpClient httpclient;
        List<NameValuePair> nameValuePairs;

        String userInfo[] = getUserInfo();
        String uname = userInfo[0];
        String pass = userInfo[1];

        if(uname != null && pass != null) {
            String bytesSent;
            httppost = new HttpPost(getURL());  
            httpclient = new DefaultHttpClient();
            nameValuePairs = new ArrayList<NameValuePair>(2);  
            String reminderstatus = "P";

            nameValuePairs.add(new BasicNameValuePair("doAction", "something"));
            nameValuePairs.add(new BasicNameValuePair("username",uname ));
            nameValuePairs.add(new BasicNameValuePair("password", pass));


            try {        
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));  
                HttpResponse response = httpclient.execute(httppost);
                InputStream is = response.getEntity().getContent();
                BufferedInputStream bis = new BufferedInputStream(is);
                ByteArrayBuffer baf = new ByteArrayBuffer(20);
                int current = 0;  

                while((current = bis.read()) != -1) {  
                    baf.append((byte)current);  
                }  

                bytesSent = new String(baf.toByteArray());
            }
        }
葬心 2024-11-09 19:00:40

您好,您能告诉我您遇到了什么异常吗?还要检查是否添加了以下权限。

<uses-permission android:name="android.permission.INTERNET" />

Hi Can you let me know what exception you are getting. Also check whether the below permission is added.

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