我创建了一个 HTTP GET 函数,该函数从我的服务器检索响应并将其显示为文本视图中的 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());
}
}
}