M
M
math_man2017-02-28 00:15:32
Android
math_man, 2017-02-28 00:15:32

Stream not working?

In the method:

public void getSMSToSend()  {
        while(flag){
            String sql = "http://80.252.241.43/telephony/android/get_sms.php?login=" + Account.login + "&password=" + Account.password;
            String text;
            synchronized (this){
                MyThread mt = new MyThread(sql);
                mt.start();
                text = mt.returnResponce();
                mt = null;
            }


            System.out.println("TEXT = " + text);
            //String text = request(sql);
            if(!text.equals("||")){
                String phone = text.substring(0, text.indexOf("|"));
                phoneNo = phone;
                text = text.substring(text.indexOf("|")+1);
                String sms = text.substring(0, text.indexOf("|"));
                message = sms;
                String idd = text.substring(text.indexOf("|")+1);
                id=idd;
                System.out.println("ID="+id);

                if(!idd.equals("")&&!sms.equals("")&&!phone.equals("")) {
                    sendSMS();
                }
            }
        }

I create a MyThread thread:
MyThread mt = new MyThread(sql);
                mt.start();
                text = mt.returnResponce();
                mt = null;

The MyThread class itself:
public class MyThread extends Thread {
    private Thread temp;
    private String responce;
    private boolean flag;
    MyThread(final String sql){
        flag = true;
        temp = new Thread(new Runnable() {
            @Override
            public synchronized void run() {
                    try {
                        OkHttpClient client = new OkHttpClient();
                        Request request = new Request.Builder().url(sql).build();
                        Response response = client.newCall(request).execute();
                        responce = response.body().string();
                        System.out.println("RESPONCE = " + responce);

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

    public synchronized String returnResponce() {
        return responce;
    }

    @Override
    public synchronized void start() {
        super.start();
    }
}

The returnResponce() method always returns null, but it should at least have a phone number there. Please tell me what could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
one pavel, 2017-02-28
@onepavel

the problem is that returnResponce is called faster than the thread is processing

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question