gpt4 book ai didi

android - JSON 服务调用不返回任何内容

转载 作者:行者123 更新时间:2023-11-30 04:12:10 25 4
gpt4 key购买 nike

我在 web 服务中调用返回字符串“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>

并确保该服务正在运行

最佳答案

Localhost127.0.0.1Android仿真设备自己的loopback接口(interface),换句话说,你连接到android仿真设备(不是您的计算机)通过使用 localhost127.0.0.1。如果您尝试访问您的开发机器,请使用 10.0.2.2.

您的代码中有一件事您没有初始化您的 StringBuilder 构建器

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

关于android - JSON 服务调用不返回任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10673773/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com