Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在线程中使用InputStream接收简单文本?安卓_Android_Sockets_Inputstream - Fatal编程技术网

Android 如何在线程中使用InputStream接收简单文本?安卓

Android 如何在线程中使用InputStream接收简单文本?安卓,android,sockets,inputstream,Android,Sockets,Inputstream,我正在运行一个服务器,它会在成功连接后向新客户机写入一条简单的欢迎消息! 现在我确信我的android设备连接到服务器,但我无法接收服务器发送的欢迎文本。 我是套接字编程新手,我正在寻找一个统一的解决方案。 我将粘贴我的代码,其中有一个成功的outputStream片段,并将android连接到服务器。我将把InputStream部分保留为黑色,因为我需要解决方案 private ArrayBlockingQueue<Integer> mQueue = new ArrayBlocki

我正在运行一个服务器,它会在成功连接后向新客户机写入一条简单的欢迎消息! 现在我确信我的android设备连接到服务器,但我无法接收服务器发送的欢迎文本。 我是套接字编程新手,我正在寻找一个统一的解决方案。 我将粘贴我的代码,其中有一个成功的outputStream片段,并将android连接到服务器。我将把InputStream部分保留为黑色,因为我需要解决方案

private ArrayBlockingQueue<Integer> mQueue = new ArrayBlockingQueue<Integer>(100);
private AtomicBoolean mStop = new AtomicBoolean(false);

private OutputStream mOutputStream = null;
private InputStream mInputStream = null;

private Socket mSocket = null;

private static Thread sNetworkThread = null;
private final Runnable mNetworkRunnable = new Runnable() {

        log("starting network thread");
        String encoding = "UTF-8";
        String output="";

    @Override
    public void run() {
        log("starting network thread");

        try {
            mSocket = new Socket(ARDUINO_IP_ADDRESS, PORT);
            mOutputStream = mSocket.getOutputStream();
            mInputStream = mSocket.getInputStream();
        } catch (UnknownHostException e1) {
            e1.printStackTrace();
            mStop.set(true);
        } catch (IOException e1) {
            e1.printStackTrace();
            mStop.set(true);
        }

        mQueue.clear(); // we only want new values

        //Output Stream
        try {
            while(!mStop.get()){
                int val = mQueue.take();
                if(val >= 0){
                    log("sending value "+val);
                    mOutputStream.write((val+"\n").getBytes());
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally{
            try {
                mStop.set(true);
                if(mOutputStream != null) mOutputStream.close();
                if(mSocket != null) mSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //InputStream
         try 
         {
            //Call method to read inputStream
            output = readFully(mInputStream,encoding);

         } catch (IOException e) {
            e.printStackTrace();
        }

        Toast.makeText(getApplicationContext(),"Server Message"+output, Toast.LENGTH_SHORT).show();

        log("returning from network thread");
        sNetworkThread = null;
    }
};
**服务器返回一个字符串-“欢迎新用户” 我在原始模式下使用putty连接到服务器,效果很好

堆栈跟踪日志:

03-02 16:46:22.790  11813-12339/com.example.bonny.myapplication D/>==< ArduinoYun >==<﹕ starting network thread
03-02 16:46:22.790  11813-12339/com.example.bonny.myapplication D/libc﹕ [NET] getaddrinfo  hn 9, servname NULL, ai_family 0+
03-02 16:46:22.790  11813-12339/com.example.bonny.myapplication D/libc﹕ [NET] ht 0x31302e302e302e
03-02 16:46:22.790  11813-12339/com.example.bonny.myapplication D/libc﹕ [NET] getaddrinfo-exit SUCCESS
03-02 16:46:22.790 11813-12339/com.example.bonny.myapplication D/>===
上面的代码可能会帮助您将InputStream转换为字符串。

我希望这会对你有所帮助。

你需要读一行。您当前的代码尝试读取所有内容,直到流结束,而这种情况仅在对等方关闭连接时发生。

显示您尝试的read()。出了什么问题?准确地说出服务器发送的内容。如果您要求ho接收,则必须显示执行该操作的代码。这是显而易见的吗?@greenapps:我已经更新了我的问题!可以是。但你当然应该告诉我现在发生了什么。readFully()返回什么?你在那片土司中看到了什么?说出流程。告诉我发生了什么事。请评论Amrut Bidri的代码。“readFully()”方法从inputStream返回“byte converted to string”格式。嗯,祝酒词永远不会出现。是的,谢谢你的建议。我已经回复了他。这将读取或阻止,直到对等方关闭连接。他只想读一个回复。@Amrut Bidri是的,这是一个很好的方法,但我的方法也有同样的实现。我认为问题在于如何调用线程内的方法!stacktraces为空…在logcat上不返回任何内容。我认为你的方法是正确的,但似乎它永远不会成功,直到方法。这是绝对正确的。你刚才说的很有道理!我在这里有点迷路了。尽我所能尝试每一步!您是否可以编写一个小片段作为示例,说明如何截取inputStream,直到它到达回车“\n”?从服务器传入的字符串是“欢迎新客户端”\n这将非常有帮助。@Amrut Bidri在((line=br.readLine())!=null)
时告诉您已经在
中了。
03-02 16:46:22.790  11813-12339/com.example.bonny.myapplication D/>==< ArduinoYun >==<﹕ starting network thread
03-02 16:46:22.790  11813-12339/com.example.bonny.myapplication D/libc﹕ [NET] getaddrinfo  hn 9, servname NULL, ai_family 0+
03-02 16:46:22.790  11813-12339/com.example.bonny.myapplication D/libc﹕ [NET] ht 0x31302e302e302e
03-02 16:46:22.790  11813-12339/com.example.bonny.myapplication D/libc﹕ [NET] getaddrinfo-exit SUCCESS
public String convertStreamToString(java.io.InputStream is)
{

    BufferedReader br = null;
    StringBuilder sb = new StringBuilder();

    String line;
    try
    {

        br = new BufferedReader(new InputStreamReader(is));
        while ((line = br.readLine()) != null)
        {
            sb.append(line);
        }

    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        if (br != null)
        {
            try
            {
                br.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    return sb.toString();
}