Android 如何从get返回结果(okhttp3)
我使用okhttp3 get方法得到一个结果。 现在,我想把结果返回给MainActivity 我试着使用intent,但失败了。 我也读过这个。但我不知道该在哪里写代码Android 如何从get返回结果(okhttp3),android,json,okhttp,Android,Json,Okhttp,我使用okhttp3 get方法得到一个结果。 现在,我想把结果返回给MainActivity 我试着使用intent,但失败了。 我也读过这个。但我不知道该在哪里写代码 public interface GetLastIdCallback { void lastId(String id); } 我的主要活动: getMaskInfo info = new getMaskInfo(this); info.requestGet(latitude, longitude); getMask
public interface GetLastIdCallback {
void lastId(String id);
}
我的主要活动:
getMaskInfo info = new getMaskInfo(this);
info.requestGet(latitude, longitude);
getMaskInfo活动(我想返回JSONObject或JSONArray):
在MainActivity中创建回调:
public void onResult(JSONArray存储)
或者你想从电话中回复什么。由于您现在知道您的mContext
实际上是MainActivity
,因此可以进行转换并调用该方法
((MainActivity)mContext).onResult(totalStore)
如果您需要将getMaskInfo
与其他活动一起使用,您可以将方法onResult
放入一个接口中,使MainActivity
实现该接口,并将该接口作为参数传递给getMaskInfo
接口类
更新onResponse函数
调用活动必须实现GetLastIdCallback接口
不是从
requestGet()
方法返回void
,而是返回LiveData
public LiveData<JSONObject> requestGet(double lat, double lng) {
LiveData<JSONObject> result = MutableLiveData<JSONObject>();
/* reqeust builder & url builder code here */
client.newCall(request).enqueue(new Callback() {
/* override other methods here */
public void onResponse(Call call, Response response) throws IOException {
try{
JSONObject jsonObject = new JSONObject(response.body().string());
((MutableLiveData) result).postValue(jsonObject);
}catch (JSONException e){
/* catch and do something */
}
}
});
return result;
}
否则,您也可以在mainactivity上实现接口,并在getMaskInfo或requestGet方法中使用其实例发回数据。尝试使用我尝试过的方法。但是我在
result.postValue(jsonObject)中遇到了一个问题代码>。错误在于:变量“result”是从内部类中访问的,需要声明为final。所以我修改了代码。然后我遇到了另一个问题:“postValue”在“androidx.lifecycle.LiveData”中保护了访问。如何解决此问题?因为我们已将result
声明为LiveData
对象,其中postValue()
受到限制;键入cast将其转换为MutableLiveData
对象,其中postValue()
不受限制。我用另一种方法解决了它。但真的非常感谢你!!
public interface GetLastIdCallback {
void lastId(String id);
void getJSONCallback(JSONObject object);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
try{
JSONObject jsonObject = new JSONObject(response.body().string());
JSONArray totalStore = jsonObject.getJSONArray("stores");
System.out.println(jsonObject);
((GetLastIdCallback )(mContext)).getJSONCallback(jsonObject); //Return here
}catch (JSONException e){
//
}
}
});
public class Main2Activity extends AppCompatActivity implements GetLastIdCallback{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
@Override
public void lastId(String id) {
}
@Override
public void getJSONCallback(JSONObject object) {
//Here you can use response according to your requirements
}
}
public LiveData<JSONObject> requestGet(double lat, double lng) {
LiveData<JSONObject> result = MutableLiveData<JSONObject>();
/* reqeust builder & url builder code here */
client.newCall(request).enqueue(new Callback() {
/* override other methods here */
public void onResponse(Call call, Response response) throws IOException {
try{
JSONObject jsonObject = new JSONObject(response.body().string());
((MutableLiveData) result).postValue(jsonObject);
}catch (JSONException e){
/* catch and do something */
}
}
});
return result;
}
info.requestGet(latitude, longitude).observe(getViewLifeCycleOwner, new Observer() {
@Override
public void onCanged(JSONObject result) {
/* code to use result */
}
});