如何解析服务器套接字输入流以获取json对象?

时间:2014-12-16 15:40:12

标签: android rest http android-json

在我的Android应用程序中,我有一个监听器线程,它监听通过服务器套接字发布的任何内容。一旦发布了某些内容(我使用HttpRequester工具发布任何消息),我就会得到输入流。我能够在输入流中看到我的json但是找不到解析方法来从中获取json对象。

以下是我的代码示例:

iMessageListenerThread = new Thread() {
            public void run(){
                // wait till user connects to server
                try
                {
                    iServerSocket = new ServerSocket();
                    iServerSocket.setReuseAddress(true);
                    iServerSocket.bind(new InetSocketAddress(mPort));
                    Log.d(TAG,"Server Socket opened" );

                    try {
                        for (;;) {
                            Socket ss = iServerSocket.accept(); // unblocks when connection is requested
                            // test if something received, if so, display this
                            InputStream localInputStream = ss.getInputStream();
                            if(localInputStream == null){
                                Log.d(TAG,"LOCAL INPUT STREAM NULL" );
                                continue;
                            }


                            //localInputStream = new GZIPInputStream(localInputStream);
                            String resultstring = convertStreamToString(localInputStream);
-----
-----


    private String convertStreamToString(InputStream is) {
        String line = "";
        StringBuilder total = new StringBuilder();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        try {
            while ((line = rd.readLine()) != null) {
                total.append(line+"\n");
                total.append(line);
            }
        } catch (Exception e) {
            Toast.makeText(this, "Stream Exception", Toast.LENGTH_SHORT).show();
        }
        return total.toString();
    }

在HttpRequester工具中,我输入以下网址: http://xxx.xxx.xx.xxx:8000/powerstate

在内容部分,我把这个json:

{ “电源状态”: “上”}


这是我的响应字符串,包含json对象:

I/RecorderService( 1518): ...resultstring : POST /powerstate HTTP/1.1
I/RecorderService( 1518): POST /powerstate HTTP/1.1Host: 192.168.21.240:8000
I/RecorderService( 1518): Host: xxx.xxx.xx.xxx:8000User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0
I/RecorderService( 1518): User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
I/RecorderService( 1518): Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Language: en-US,en;q=0.5
I/RecorderService( 1518): Accept-Language: en-US,en;q=0.5Accept-Encoding: gzip, deflate
I/RecorderService( 1518): Accept-Encoding: gzip, deflateContent-Type: application/json; charset=UTF-8
I/RecorderService( 1518): Content-Type: application/json; charset=UTF-8Content-Length: 19
I/RecorderService( 1518): Content-Length: 19Connection: keep-alive
I/RecorderService( 1518): Connection: keep-alivePragma: no-cache
I/RecorderService( 1518): Pragma: no-cacheCache-Control: no-cache
I/RecorderService( 1518): Cache-Control: no-cache
I/RecorderService( 1518): {"powerstate":"on"}

有人可以帮助我知道如何从此输入流中提取json对象

0 个答案:

没有答案