1

我在 webservice 中调用返回字符串“true”或“false”的方法,并使用以下代码获取两个参数

String isLogedin = readTwitterFeed();
public String readTwitterFeed() {

        String Result = null;

        StringBuilder URL = new StringBuilder();

        URL.append("http://localhost:1539/WCFService1/Service.svc/Login/");
        URL.append(username.getText());
        URL.append("/");
        URL.append(password.getText());

        ///////////////////////////// 
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(
                URL.toString());




        try {
            HttpResponse response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(content));

                StringBuilder builder = null ;
                String line = null;
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }

                Result = builder.toString() ;


            } else {
                 Result = "error";
            }
        } catch (ClientProtocolException e) {
             Result = "error";
            e.printStackTrace();
        } catch (IOException e) {
             Result = "error";
            e.printStackTrace();
        }
        return Result;
    }

但返回的字符串始终为空

知道为什么它不返回数据,也不显示任何错误(我在 textview 上显示返回值),我也设置了这样的互联网权限

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

并确定该服务正在运行

4

1 回答 1

1

Localhost并且127.0.0.1Android emulated device's自己的环回接口,换句话说,您使用localhostor连接到android模拟设备(而不是您的计算机) 127.0.0.1。如果您尝试访问您的开发机器,请使用10.0.2.2.

你的代码中的一件事你没有初始化你的StringBuilder builder

像这样初始化它StringBuilder builder = new StringBuilder();

于 2012-05-20T16:47:32.540 回答