gpt4 book ai didi

android - JSON解析成textview Android

转载 作者:可可西里 更新时间:2023-11-01 17:11:07 26 4
gpt4 key购买 nike

我创建了一个 HTTP GET 函数,它从我的服务器检索响应并将其显示为 TextView 中的 JSON。

我怎样才能将以下内容变成一个字符串以供我的 TextView 读取。

{"response":"ok"}

这是我的 HTTP GET 请求

public class GetMethodEx {

public String getInternetData() throws Exception {


BufferedReader in = null;
String data = null;

try {
HttpClient client = new DefaultHttpClient();
client.getConnectionManager().getSchemeRegistry().register(getMockedScheme());

URI website = new URI("https://server.com:8443/login?username=hm&password=123");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
response.getStatusLine().getStatusCode();

in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) != null) {
sb.append(l + nl);
}
in.close();
data = sb.toString();
return data;
} finally {
if (in != null) {
try {
in.close();
return data;
} catch (Exception e) {
Log.e("GetMethodEx", e.getMessage());
}
}
}

最佳答案

使用下面的代码解析json数据。

JSONObject jObject;
try {
jObject = new JSONObject(data);
String mResponse = jObject.getString("response");
mTxtView1.setText(mResponse);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

关于android - JSON解析成textview Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13744477/

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