我需要从onPostExecute(异步任务(为了绘制图表,从var s(通信)到onCreatView)。 这是我的代码: 我尝试了许多解决方案,但我做不到。
String url;
HashMap<String, String> params;
int requestCode;
PerformNetworkRequest3(String url, HashMap<String, String> params, int requestCode) {
this.url = url;
this.params = params;
this.requestCode = requestCode;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
JSONObject object = new JSONObject(s);
if (!object.getBoolean("error")) {
Toast.makeText(getContext(), object.getString("message"), Toast.LENGTH_SHORT).show();
s = (object.getString("category")); //need var s -> oncreateview
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(Void... voids) {
RequestHandler requestHandler = new RequestHandler();
if (requestCode == CODE_POST_REQUEST)
return requestHandler.sendPostRequest(url, params);
if (requestCode == CODE_GET_REQUEST)
return requestHandler.sendGetRequest(url);
return null;
}
答案 0 :(得分:0)
只需创建一个回调接口。最终解决方案是。
new PerformNetworkRequest3("", new HashMap<>(), 0, new PerformNetworkRequest3.OnResult() {
@Override
public void onSuccess(String s) {
// check response here.
}
@Override
public void onError() {
}
});
并在下面替换为您的AsyncTask
import android.os.AsyncTask;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
/**
* Created by KHEMRAJ on 7/23/2018.
*/
public class PerformNetworkRequest3 extends AsyncTask<Void, Void, String> {
String url;
HashMap<String, String> params;
int requestCode;
private OnResult onResult;
private interface OnResult{
void onSuccess(String s); // you can change this String to any, like JsonObject
void onError();
}
PerformNetworkRequest3(String url, HashMap<String, String> params, int requestCode, OnResult onResult) {
this.url = url;
this.params = params;
this.requestCode = requestCode;
this.onResult = onResult;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
JSONObject object = new JSONObject(s);
onResult.onSuccess(s);
if (!object.getBoolean("error")) {
Toast.makeText(getContext(), object.getString("message"), Toast.LENGTH_SHORT).show();
s = (object.getString("category")); //need var s -> oncreateview
}
} catch (JSONException e) {
e.printStackTrace();
onResult.onError();
}
}
@Override
protected String doInBackground(Void... voids) {
RequestHandler requestHandler = new RequestHandler();
if (requestCode == CODE_POST_REQUEST)
return requestHandler.sendPostRequest(url, params);
if (requestCode == CODE_GET_REQUEST)
return requestHandler.sendGetRequest(url);
return null;
}
}
这只是一个实现。您可以根据需要更改回调方法。还要正确管理onError()
和onSucess()
。
答案 1 :(得分:0)
尝试一下:-
<div class="field-group" *ngFor="let mworkq of selectq">
<div class="label-cust">{{mworkq.Question}}</div>
<div class="field-cust">
<select [(ngModel)]="selectedOption">
<option value=''>Select</option>
<option [hidden]="mworkq.option == null || mworkq.option.length == 0" *ngFor="let mworkq of mworkq.options" value="{{mworkq.option}}">{{mworkq.option}}</option>
<!-- <option value="{{mworkq.option4}}">{{mworkq.option4}}</option> -->
</select>
</div>
</div>
<div class="field-group" *ngFor="let mworkq of textq">
<div class="label-cust">
{{mworkq.Question}}
<ion-input [hidden]="mworkq.option == null || mworkq.option.length == 0" *ngFor="let mworkq of mworkq.options" class="{{mworkq.option}}" type="number" placeholder="{{mworkq.option}}" ></ion-input>
</div>
</div>
在主要活动中,您应该实现接口AsyncResponse并重写其方法以获取值。
MainActivity.java
String url;
HashMap<String, String> params;
int requestCode;
public AsyncResponse delegate=null;
public interface AsyncResponse {
void processFinish(String output);
}
PerformNetworkRequest3(Context ctx, AsyncResponse asyncResponse) {
context = ctx;
delegate = asyncResponse;
}
PerformNetworkRequest3(String url, HashMap<String, String> params, int requestCode) {
this.url = url;
this.params = params;
this.requestCode = requestCode;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
JSONObject object = new JSONObject(s);
if (!object.getBoolean("error")) {
Toast.makeText(getContext(), object.getString("message"), Toast.LENGTH_SHORT).show();
s = (object.getString("category")); //need var s -> oncreateview
delegate.processFinish(s);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(Void... voids) {
RequestHandler requestHandler = new RequestHandler();
if (requestCode == CODE_POST_REQUEST)
return requestHandler.sendPostRequest(url, params);
if (requestCode == CODE_GET_REQUEST)
return requestHandler.sendGetRequest(url);
return null;
}