如何将 JSON 响应转换为字符串并显示在屏幕上?
我已经编写了 android 类,它将调用 RESTful Web 服务。如果请求成功, 响应将是 JSON 对象。我这样写 android 类:
public class android extends Activity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView txt = (TextView) findViewById(R.id.textView1);
txt.setText(getInputStreamFromUrl("http://localhost:8080/kyaw"));
}
public static String getInputStreamFromUrl(String url) {
InputStream content = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(url));
content = response.getEntity().getContent();
} catch (Exception e) {
Log.e("[GET REQUEST]", "Network exception");
}
String result=convert(content);
return result;
}
private static String convert(InputStream in)
{
BufferedReader reader=new BufferedReader(new InputStreamReader(in));
StringBuilder sb=new StringBuilder();
String line=null;
try{
while((line=reader.readLine())!=null){
sb.append(line+"\n");
}
}catch(Exception e)
{
e.printStackTrace();
}finally{
try{
in.close();
}catch(IOException e){
e.printStackTrace();
}
}
return sb.toString();
}
}
我运行后遇到问题。我会得到异常,因为我作为字符串返回。但响应是 JSON。我应该如何将 JSON 转换为字符串或其他方式,然后如何在android屏幕上显示结果?
谢谢
I have written android class which will call RESTful web service. If request is successful,
response will be JSON object. I write android class like this:
public class android extends Activity {
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView txt = (TextView) findViewById(R.id.textView1);
txt.setText(getInputStreamFromUrl("http://localhost:8080/kyaw"));
}
public static String getInputStreamFromUrl(String url) {
InputStream content = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(url));
content = response.getEntity().getContent();
} catch (Exception e) {
Log.e("[GET REQUEST]", "Network exception");
}
String result=convert(content);
return result;
}
private static String convert(InputStream in)
{
BufferedReader reader=new BufferedReader(new InputStreamReader(in));
StringBuilder sb=new StringBuilder();
String line=null;
try{
while((line=reader.readLine())!=null){
sb.append(line+"\n");
}
}catch(Exception e)
{
e.printStackTrace();
}finally{
try{
in.close();
}catch(IOException e){
e.printStackTrace();
}
}
return sb.toString();
}
}
I have a problem after i run.I will get exception coz i return as a string.But response is JSON .How should I convert JSON to String or other way and then how to show result in android screen?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么“http://localhost:8080/kyaw”是一个问题...通过这个你指向模拟器而不是模拟器的主机...并且你收到网络错误
尝试“http://ip.of.your.host:8080 /kyaw”
编辑:
well "http://localhost:8080/kyaw" is a problem ... by this you poining to emulator not emulator's host ... and you getting network error
try "http://ip.of.your.host:8080/kyaw"
EDIT:
我只是这样做:
I just do this: