android中的Web服务问题

时间:2011-04-21 06:45:03

标签: android web-services httpclient

我使用代码

调用json webservice
HttpClient client = new DefaultHttpClient();
  HttpGet method = new HttpGet(Url);
  HttpResponse response = client.execute(method);

执行最后一行后,应用程序始终捕获异常。

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

你能告诉我你得到的例外情况吗?同时检查是否添加了以下权限。

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