在Android Studio中将数据从python服务器接收到客户端-Java

时间:2019-04-16 19:25:21

标签: java android python

我正在使用websockets以便将字符串从python服务器传递到android studio java客户端。我检查了服务器发送的字符串(通过在python中打印),该字符串有效,所以我想问题是出在Java中如何获取数据。

我需要发送以下内容: 我在python中有一个保存数据的列表。我不知道她的身长,因此我使用len(my_list)进行发送。可以肯定的是,我在列表中甚至有单元格,每次都需要发送双倍的单元格。那是我的Java代码:

服务器处理程序:

package cyber_pro4.com;


import android.content.Context;
import android.widget.Toast;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;

public class ServerHandler
{

    private OkHttpClient client;
    private WebSocket ws;
    private WebSocketListener listener;

    private Context context;

    private String message = "";

    public ServerHandler(Context context) {
        this.context = context;
        init();
    }

    private void init() {
        client = new OkHttpClient();
        listener = new SocketListener();
    }

    public void start() {
        Request request = new Request.Builder().url("ws://10.0.0.22:8080").build();
        ws = client.newWebSocket(request, listener);
        Toast.makeText(context, "connected to server", Toast.LENGTH_SHORT).show();
    }

    public void sendMessage(String message) {
        ws.send(message);
    }

    public String getMessage() {
        System.out.println("BATMAN GET " + message);
        String msg = message;
        message = "";
        return msg;
    }

    public void resetMessage() { message = ""; }

    public void closeConnection() {
        client.dispatcher().executorService().shutdown();
    }

    private final class SocketListener extends WebSocketListener {

        @Override
        public void onMessage(WebSocket webSocket, String text) {
            message = text;
        }

        @Override
        public void onClosing(WebSocket webSocket, int code, String reason) {
            webSocket.close(1000, null);
        }
    }

}



我获取数据的方式:

       ServerHandler serverHandler2;
       serverHandler2 = new ServerHandler(Fish_info.this);
       serverHandler2.start();
       serverHandler2.sendMessage("send pic of fish"); 

       String message,message2;

       int i = 1;

       while (i > 0)
       {

           message = "";
           while ((message = serverHandler2.getMessage()).equals("")) {
           } //this string 

           if (!(message.equals("false"))) // if the server return false- that mean this is the end of my list
           {
               message2 = "";
               while ((message2 = serverHandler2.getMessage()).equals("")) {
               } //this string is a info

              Toast.makeText(this, message, Toast.LENGTH_LONG).show();
              Toast.makeText(this, message2, Toast.LENGTH_LONG).show();


           }
           else
           {
               Toast.makeText(this, "There you go- the end of the list", Toast.LENGTH_LONG).show();
               i = -5;
           }

       }

即使列表有4个单元格,我也只能烘烤列表的前两个单元格。 为什么会这样?

1 个答案:

答案 0 :(得分:0)

我成功地做到了这一点,对于那些将来需要它的人来说,我刚刚做了一个serverHandler2.sendMessage("send pic of fish");,然后才从服务器中获取所有数据。 (以message = ""; while ((message = serverHandler2.getMessage()).equals("")) { } //this string的代码为准)