如何从onResponse获取返回值?

时间:2018-10-28 17:04:23

标签: class methods httprequest return-value

我试图查看我是否可以使onResponse返回一个值,以便可以将其用作方法,因此可以在我的主要活动中多次使用它,但是我无法使onResponse返回一个值,并且我不理解我已经看到人们使用“回调”这个概念来解决类似的问题...我认为这仅仅是因为我对Java不熟悉并且仍然在学习-有人可以帮助我吗?

谢谢

类:Util.java

package com.example.mkm.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Util extends AppCompatActivity

{
    /**private String OK;*/
    protected void onCreate (Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    OkHttpClient client = new OkHttpClient();

    String url = "http://192.168.43.33/onled";

    Request request = new Request.Builder()
            .url(url)
            .build();

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();
        }


        public void onResponse(Call call, Response response) throws IOException {
            if (response.isSuccessful()) {
                /** How do i return the result?*/ 
               /** OK = response.body().string();*/
                }


        }});
}


}

然后在我的主线程中

package com.example.mkm.myapplication;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

private TextView mTextViewResult;
private ToggleButton but1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTextViewResult = findViewById(R.id.text_view_result);
}

public void toggleClick(View view) {
    but1 = findViewById(R.id.colorChange);
    boolean on = ((ToggleButton) view).isChecked();
    /**Here i'd like to call the class' method to see if the HTTP request succesfully interacted and then use the return value to turn on my button*/
    if (on) {
        but1.setBackgroundColor(Color.GREEN);

    }
    else {
        but1.setBackgroundColor(Color.GRAY);
    }
}
}

1 个答案:

答案 0 :(得分:0)

事实证明,我只是一个预期的人!

似乎我所缺少的只是声明该类,并使用以下命令在我的mainActivity中创建

enter code here

toggleGPIO turnOn = new toggleGPIO();

所以我的代码现在已更改为;

MainActivity;

package com.example.mkm.myapplication;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

private TextView mTextViewResult;
private ToggleButton but1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mTextViewResult = findViewById(R.id.text_view_result);
}

public void toggleClick(View view) {
    but1 = findViewById(R.id.colorChange);
    boolean on = ((ToggleButton) view).isChecked();
    toggleGPIO turnOn = new toggleGPIO();
    if (on) {
        turnOn.toggleGPIO("onled");
        but1.setBackgroundColor(Color.GREEN);
    }
    else{
        turnOn.toggleGPIO("offled");
            but1.setBackgroundColor(Color.GRAY);
        }
    }
}

然后是方法;

package com.example.mkm.myapplication;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

class toggleGPIO {

String toggleGPIO(String http) {

    OkHttpClient client = new OkHttpClient();
    final  String myString = "true";
    String url = "http://192.168.43.33/"+http;

    Request request = new Request.Builder()
            .url(url)
            .build();

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();
        }

        public void onResponse(Call call , Response response){
            if (response.isSuccessful()) {
/** 
        Only problem is i dont actually get a return result from this here because i still dont know how to use "Callbacks"
return myString;
*/
                               }
            }

    });
    return myString;
}

}

但是我仍然想知道如果有人有线索,如何在功能中使用Calback。