如何将 JSON 响应转换为字符串并显示在屏幕上?

发布于 2024-11-08 05:37:27 字数 1676 浏览 0 评论 0原文

我已经编写了 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?

thanksenter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薯片软お妹 2024-11-15 05:37:27

那么“http://localhost:8080/kyaw”是一个问题...通过这个你指向模拟器而不是模拟器的主机...并且你收到网络错误

尝试“http://ip.of.your.host:8080 /kyaw”

编辑:

        content = response.getEntity().getContent();// here comes an error
      } catch (Exception e) {
        Log.e("[GET REQUEST]", "Network exception");//we catch it here
      }
      //here you got content == null
      //so you getting null point exception 

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:

        content = response.getEntity().getContent();// here comes an error
      } catch (Exception e) {
        Log.e("[GET REQUEST]", "Network exception");//we catch it here
      }
      //here you got content == null
      //so you getting null point exception 
×纯※雪 2024-11-15 05:37:27

我只是这样做:

HttpClient client = new DefaultHttpClient();
HttpPost poster = new HttpPost("http://www.example.com/json.php");
poster.addHeader("Content-Type", "text/json");
poster.setEntity(new StringEntity(data.toString()));
//data being a json object created and filled earlier
HttpResponse response = client.execute(poster);                                 
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
    DataInputStream input = new DataInputStream(response.getEntity().getContent());
    JSONObject json = new JSONObject(input.readLine());
    json.toString(2);
}

I just do this:

HttpClient client = new DefaultHttpClient();
HttpPost poster = new HttpPost("http://www.example.com/json.php");
poster.addHeader("Content-Type", "text/json");
poster.setEntity(new StringEntity(data.toString()));
//data being a json object created and filled earlier
HttpResponse response = client.execute(poster);                                 
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
    DataInputStream input = new DataInputStream(response.getEntity().getContent());
    JSONObject json = new JSONObject(input.readLine());
    json.toString(2);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文