我无法从服务器发送数据

时间:2017-01-23 06:04:02

标签: java printwriter

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();
}

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

2 个答案:

答案 0 :(得分:1)

关闭I have some border issue. border-bottom is working fine but border-right not working good, I don't know why this code behaving like this way , if i change the color code , its working fine take look here please enter image description here Hide code snippet .activities h2 { font-size: 26px; color: #0e5e98; font-family: 'Reef-Bold'; padding: 10px 0; } .activities p { font-size: 14px; color: #000; font-family: 'open_sansregular'; display: block; padding: 10px 0; line-height: 21px; } .activities a { font-size: 16px; color: #0e5e98; font-family: 'Reef-Bold'; padding: 10px 0; } .activities a:hover { color: #e72129; } .activities .col-lg-6 { margin: 30px 0 0 0; padding-bottom: 30px; border-right: 8px solid #dfe4e3; } .activities .col-lg-6:first-child, .activities .col-lg-6:nth-child(2) { border-bottom: 8px solid #dfe4e3; } .activities .col-lg-6:nth-child(even) { border-right: none; }关闭<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <div class="container activities"> <div class="row"> <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 w100 text-center"> <img src="images/kiddies_pool.png" alt="kiddies_pool" /> <h2>Kiddies Pool</h2> <p>Future Kid Entertainment & Real Estate Company is a Kuwaiti shareholding company (K.S.C.C.) with a capital of over..</p> <a href="#">Read More</a> </div> <!-- /.col-lg-6 --> <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 w100 text-center"> <img src="images/speed_pool.png" alt="speed slide" /> <h2>Speed Slide</h2> <p>Future Kid Entertainment & Real Estate Company is a Kuwaiti shareholding company (K.S.C.C.) with a capital of over..</p> <a href="#">Read More</a> </div> <!-- /.col-lg-6 --> <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 w100 text-center"> <img src="images/shot_gun_slide.png" alt="shot gun" /> <h2>Shot Gun Slide</h2> <p>Future Kid Entertainment & Real Estate Company is a Kuwaiti shareholding company (K.S.C.C.) with a capital of over..</p> <a href="#">Read More</a> </div> <!-- /.col-lg-6 --> <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 w100 text-center"> <img src="images/cannon_ball_ride.png" alt="cannon" /> <h2>Cannon Ball Ride</h2> <p>Future Kid Entertainment & Real Estate Company is a Kuwaiti shareholding company (K.S.C.C.) with a capital of over..</p> <a href="#">Read More</a> </div> <!-- /.col-lg-6 --> </div> </div>关闭BufferedReader关闭InputStreamReader。写完数据后关闭InputStream

答案 1 :(得分:0)

两个可能的问题:

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

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

  

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

提示/注意:由于PrintWriter的方法从不抛出任何I / O异常,因此不会抛出异常。如果您希望被告知任何异常,或者至少检查是否存在错误,我建议不要使用该类 - 方法checkError()。见javadoc PrintWriter

  

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