Android OnPostExecute从未触发
我读过很多问题,比如我关于stackoverflow的问题,但我没有找到任何解决问题的方法。问题是我在onClick中使用Android OnPostExecute从未触发,android,methods,android-asynctask,runtime-error,Android,Methods,Android Asynctask,Runtime Error,我读过很多问题,比如我关于stackoverflow的问题,但我没有找到任何解决问题的方法。问题是我在onClick中使用.execute()方法调用AsynkTask。doInBackground完成,但之后不会调用onPostExecute,即使上面有@override,并且doInBackground返回一个值。我在班上发帖 public class FragmentControllaCorsa extends Fragment implements View.OnClickListene
.execute()
方法调用AsynkTask
。doInBackground
完成,但之后不会调用onPostExecute
,即使上面有@override
,并且doInBackground
返回一个值。我在班上发帖
public class FragmentControllaCorsa extends Fragment implements View.OnClickListener{
private Button controlla;
private TextView ultimaCorsa, validitaCorsa;
private EditText codiceDocumento;
private Context context;
// Tag usato nei log.
// Tag used into logs.
private static final String TAG = FragmentControllaCorsa.class.getName();
// Test dall'Emulatore:
// Testing from Emulator:
private static final String LOGIN_URL = "http://10.0.2.2/PrimaAppAndroid/get/ultimaTratta.php";
// Tag(ID) del responso Json restituito dallo script php:
// JSON element ids from repsonse of php script:
private static final String TAG_SUCCESS = "success";
private static final String TAG_DATA = "Data";
private static final String TAG_DURATA = "DurataConvalida";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/*
Inserisco il layout di questo fragment.
--
Inflate the layout for this fragment
*/
return inflater.inflate(R.layout.fragment_controlla_corsa, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
/*
Creo Il bottone controlla e vi associo come listener questa classe stessa.
--
I create the button controlla and I associate him this class as listnener.
*/
controlla = (Button) getView().findViewById(R.id.buttonControllaCorsa);
controlla.setOnClickListener(this);
/*
Ottengo le due edit text dal file .xml, il context dell'activity e l'EditText codiceDocumento.
--
I get the two EditText from the .xml file, the activity context and the EditText codiceDocumento.
*/
ultimaCorsa = (TextView) getView().findViewById(R.id.ultimaCorsaControlloCorsa);
validitaCorsa = (TextView) getView().findViewById(R.id.validitaCorsaControlloCorsa);
context = getActivity();
codiceDocumento = (EditText) getView().findViewById(R.id.codiceDocumentoControllaCorsa);
}
@Override
public void onClick(View view) {
new DownloadUltimaCorsa().execute();
}
class DownloadUltimaCorsa extends AsyncTask<Void, Void, JSONObject>{
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setMessage("Download ultima tratta...");
// Dura un tempo indeterminato.
// It lasts a undefined time.
pDialog.setIndeterminate(false);
// E' cancellabile dal tasto Back.
// It's cancelable by the Back key.
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(Void... voids) {
// Per il controllo se è andato a buon fine.
// Checking if all goes well.
int success;
// Il parametro da passare.
// The param to pass.
String codice = codiceDocumento.getText().toString();
JSONParser jsonParser = new JSONParser();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>(); //http://stackoverflow.com/a/17609232/2337094 Namevaluepair
params.add(new BasicNameValuePair("codice", codice));
// Avvio della richiesta.
Log.d("request!", "starting");
// Start the request.
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
// Controllo del respondo json
// Check your log for json response
Log.d("Download effettuato", json.toString());
return json;
// json success tag
/*success = json.getInt(TAG_SUCCESS);
if (success == 1) {//SE VORRÒ GESTIRE L'ERRORE.
return json;
} else {
return json;
}*/
}
@Override
protected void onPostExecute(JSONObject json) {
super.onPostExecute(json);
// Chiudo il ProgressDialog.
// Dismiss the ProgressDialog.
pDialog.dismiss();
try {
JSONArray valore = new JSONArray(json.toString());
JSONObject riga = valore.getJSONObject(0);
ultimaCorsa.setText(riga.getString(TAG_DATA));
validitaCorsa.setText(riga.getString(TAG_DURATA));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
公共类FragmentControlAcorsa扩展片段实现View.OnClickListener{
专用按钮控制器;
私人TextView ultimaCorsa,validitaCorsa;
私人编辑文本文件;
私人语境;
//标记usato nei日志。
//用于日志的标记。
private static final String TAG=fragmentControlAcorsa.class.getName();
//测试日期:
//从模拟器进行测试:
私有静态最终字符串登录\u URL=”http://10.0.2.2/PrimaAppAndroid/get/ultimaTratta.php";
//标记(ID)del responso Json restituito dallo脚本php:
//php脚本repsonse中的JSON元素ID:
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串标记_DATA=“DATA”;
私有静态最终字符串标记_DURATA=“DurataConvalida”;
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
/*
Inserisco-il布局图(请参见片段)。
--
为该碎片膨胀布局
*/
返回充气机。充气(右布局。碎片控制,容器,假);
}
@凌驾
已创建视图上的公共void(视图,捆绑保存状态){
super.onViewCreated(视图,savedInstanceState);
/*
这是一个非常重要的问题。
--
我创建了button controlla,并将他与这个类关联为Listener。
*/
ControlA=(按钮)getView().findViewById(R.id.buttonControllaCorsa);
controlla.setOnClickListener(此);
/*
Ottengo le due edit text dal file.xml,il context dell'activity e l'EditText codiceDocumento。
--
我从.xml文件中获得两个EditText,即活动上下文和EditText codiceDocumento。
*/
ultimaCorsa=(TextView)getView().findViewById(R.id.UltimacorsaControlOcRSA);
validitaCorsa=(TextView)getView().findViewById(R.id.validitaCorsaControlloCorsa);
context=getActivity();
codiceDocumento=(EditText)getView().findViewById(R.id.codiceDocumentoControllaCorsa);
}
@凌驾
公共void onClick(视图){
新下载的ultimacorsa().execute();
}
类下载Ultimacorsa扩展异步任务{
私人对话;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(上下文);
pDialog.setMessage(“下载ultima tratta…”);
//不确定的节奏。
//它会持续一段未定义的时间。
pDialog.setUndeterminate(假);
//可取消的dal tasto回来了。
//可以用后退键取消。
pDialog.setCancelable(真);
pDialog.show();
}
@凌驾
受保护的JSONObject doInBackground(无效…无效){
//每名控制员将被处以罚款。
//检查是否一切顺利。
成功;
//帕萨雷参数酒店。
//参数将通过。
字符串codice=codiceDocumento.getText().toString();
JSONParser JSONParser=新的JSONParser();
//建筑参数
List params=new ArrayList()//http://stackoverflow.com/a/17609232/2337094 名称值对
参数添加(新的BasicNameValuePair(“codice”,codice));
//阿维奥·德拉·里希斯塔。
Log.d(“请求!”,“启动”);
//启动请求。
JSONObject json=jsonParser.makeHttpRequest(
登录URL,“POST”,参数);
//Controllo del respondo json
//检查日志中的json响应
Log.d(“下载effettuato”,json.toString());
返回json;
//json成功标记
/*success=json.getInt(TAG_success);
如果(success==1){//SE VORRÒGESTIRE L'ERRORE。
返回json;
}否则{
返回json;
}*/
}
@凌驾
受保护的void onPostExecute(JSONObject json){
onPostExecute(json);
//千岛日报。
//关闭ProgressDialog。
pDialog.disclose();
试一试{
JSONArray valore=新的JSONArray(json.toString());
JSONObject riga=valore.getJSONObject(0);
ultimaCorsa.setText(riga.getString(TAG_数据));
validitaCorsa.setText(riga.getString(TAG_DURATA));
}捕获(JSONException e){
e、 printStackTrace();
}
}
}
}
为什么不调用onPostExecute()?
提前谢谢你 在onPostExecute中登录一些内容并检查logcat。在
doInbackground
onPostExecute之后callled@Raghunandan是的,你说得对!但现在的问题是,为什么两个textView的文本没有改变?问题可能在解析中。在onPostExecute中记录json并将其发布到此处