我无法从服务器发送数据

时间:2022-06-25 06:13:27
BufferedReader br = new BufferedReader(new InputStreamReader(nsocket.getInputStream()));
PrintWriter out = new PrintWriter(new OutputStreamWriter(nsocket.getOutputStream()),true);

list.clear();
while ((data=br.readLine())!=null){
    list.add(data);
}

br.close();
if (list.size() >= 2) {
    lat =  list.get(0);
    log =  list.get(1);
    route =  list.get(2);
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            activity.client.setText(lat);
            activity.client1.setText(log);
        }
    });
    activity.mydb.updateData(lat, log, route);
}   else {
    parentroute = list.get(0);
    Cursor res = activity.mydb.getData(parentroute);
    while (res.moveToNext()) {
        latitude = res.getString(0);
        longitude = res.getString(1);
    }

    out.println(latitude);
    out.println(longitude);
    out.flush();
    out.close();


    nsocket.close();
}

I can receive the data.But I cant send the data using Printwriter.Using this code I can retrive data from the database using Cursor.But the retrived data can't send to the client.

我可以收到数据。但是我无法使用Printwriter发送数据。使用此代码我可以使用Cursor从数据库中检索数据。但是,已检索的数据无法发送到客户端。

2 个解决方案

#1


1  

Closing the BufferedReader closes the InputStreamReader closes the InputStream closes the Socket. Close the BufferedReader after you wrote the data.

关闭BufferedReader会关闭InputStreamReader,关闭InputStream关闭Socket。写入数据后关闭BufferedReader。

#2


0  

Two possible problems:

两个可能的问题:

1 - you are reading from the socket until the input stream is closed - are you sure the client is only closing his output and not the whole socket (connection)?

1 - 您正在读取套接字直到输入流关闭 - 您确定客户端只关闭其输出而不是整个套接字(连接)吗?

2 - see J. Tennié's asnwer, that is, when you close the input br.close(); the socket also get closed and you will not be able to send over it. See documentation of getInputStream:

2 - 见J.Tennié的asnwer,也就是当你关闭输入时br.close();套接字也会关闭,你将无法发送它。请参阅getInputStream的文档:

Closing the returned InputStream will close the associated socket.

关闭返回的InputStream将关闭关联的套接字。

Hint/note: There is no exceptions being thrown since the methods of PrintWriter never throw any I/O exception. I recommend not to use that class if you want to be informed of any exception, or, at least, check if there was an error - method checkError(). See javadoc PrintWriter:

提示/注意:抛出没有异常,因为PrintWriter的方法从不抛出任何I / O异常。如果您想要被告知任何异常,或者至少检查是否存在错误,我建议不要使用该类 - 方法checkError()。请参阅javadoc PrintWriter:

Methods in this class never throw I/O exceptions, although some of its constructors may. The client may inquire as to whether any errors have occurred by invoking checkError().

这个类中的方法永远不会抛出I / O异常,尽管它的一些构造函数可能会。客户端可以通过调用checkError()来查询是否发生了任何错误。

#1


1  

Closing the BufferedReader closes the InputStreamReader closes the InputStream closes the Socket. Close the BufferedReader after you wrote the data.

关闭BufferedReader会关闭InputStreamReader,关闭InputStream关闭Socket。写入数据后关闭BufferedReader。

#2


0  

Two possible problems:

两个可能的问题:

1 - you are reading from the socket until the input stream is closed - are you sure the client is only closing his output and not the whole socket (connection)?

1 - 您正在读取套接字直到输入流关闭 - 您确定客户端只关闭其输出而不是整个套接字(连接)吗?

2 - see J. Tennié's asnwer, that is, when you close the input br.close(); the socket also get closed and you will not be able to send over it. See documentation of getInputStream:

2 - 见J.Tennié的asnwer,也就是当你关闭输入时br.close();套接字也会关闭,你将无法发送它。请参阅getInputStream的文档:

Closing the returned InputStream will close the associated socket.

关闭返回的InputStream将关闭关联的套接字。

Hint/note: There is no exceptions being thrown since the methods of PrintWriter never throw any I/O exception. I recommend not to use that class if you want to be informed of any exception, or, at least, check if there was an error - method checkError(). See javadoc PrintWriter:

提示/注意:抛出没有异常,因为PrintWriter的方法从不抛出任何I / O异常。如果您想要被告知任何异常,或者至少检查是否存在错误,我建议不要使用该类 - 方法checkError()。请参阅javadoc PrintWriter:

Methods in this class never throw I/O exceptions, although some of its constructors may. The client may inquire as to whether any errors have occurred by invoking checkError().

这个类中的方法永远不会抛出I / O异常,尽管它的一些构造函数可能会。客户端可以通过调用checkError()来查询是否发生了任何错误。