JSON服务调用什么都不返回

时间:2012-05-20 13:23:03

标签: android json

我在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>

并确定该服务正在运行

1 个答案:

答案 0 :(得分:1)

Localhost127.0.0.1Android emulated device's自己的环回接口,换句话说,您使用localhost或{{连接到Android模拟设备(而不是您的计算机) 1}}。如果您尝试访问开发计算机,请使用127.0.0.1

您的代码中有一件事没有初始化您的10.0.2.2.

将其初始化为StringBuilder builder