gpt4 book ai didi

java - 我如何应用alertDialog作为AsyncTask中的服务器响应

转载 作者:太空宇宙 更新时间:2023-11-04 10:57:43 25 4
gpt4 key购买 nike

我正在尝试通过 cPanel 托管将我的项目 app 与在线数据库连接。我使用了AlertDialog.BuildersetOnClickListener、用于显示服务器的响应,但似乎我遇到了一些错误,任何答案将不胜感激,谢谢

这是我的 java 文件。

//Calling my AsyncTask
@Override
public void onClick(View view) {
view.setEnabled(false);
Intent launchactivity = new Intent(MainActivity.this,MainActivity.class);
startActivity(launchactivity);

tvscancontent = tvScanContent.getText().toString();
tvscanformat = tvScanFormat.getText().toString();
BackgroundTask backgroundTask = new BackgroundTask();
backgroundTask.execute(tvscancontent,tvscanformat);
finish();

showShowView2();
// displaynotification();

}

});

private class BackgroundTask extends AsyncTask<String,Void,String> { // i got crash here

Context context;
String add_info_url;
private Object activity;




public BackgroundTask() {

}

@Override
protected void onPreExecute() {


add_info_url = "http://www.d48.asia/shan/index.php";

progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("Processing...");
progressDialog.setMessage("Authenticating...");
progressDialog.setCancelable(false);
progressDialog.setIndeterminate(true);
progressDialog.setProgressStyle(progressDialog.STYLE_SPINNER);
progressDialog.show();



}

@Override
protected String doInBackground(String... args) {

String tvscancontent = args[0];
String tvscanformat = args[1];
String response = "";
String line = "";

try {
URL url = new URL(add_info_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String data_string = URLEncoder.encode("tvscancontent", "UTF-8") + "=" + URLEncoder.encode(tvscancontent, "UTF-8")+ "&" +
URLEncoder.encode("tvscanformat", "UTF-8") + "=" + URLEncoder.encode(tvscanformat, "UTF-8");
bufferedWriter.write(data_string);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));

while ((line = bufferedReader.readLine()) != null){

response+= line;


}


bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();

return response;


} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


return null;


}

@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);

}

//Server response
@Override
protected void onPostExecute(final String result) {
//Toast toast =Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG);
//toast.setGravity(Gravity.CENTER ,0,0);
//toast.show();
AlertDialog.Builder alert = new AlertDialog.Builder(context); // also i got crash here
alert.setTitle("Third Wheel Response");
alert.setMessage(result);
alert.setCancelable(false);
alert.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
AlertDialog dialogs = alert.create();
dialogs.show();


if (progressDialog != null) {
progressDialog.dismiss();
SaveData.setEnabled(true);

}

错误:

11-10 08:40:23.174 4815-4815/com.thethirdwheelproject.thirdwheel E/AndroidRuntime:致命异常:main 进程:com.thethirdwheelproject.thirdwheel,PID:4815 android.view.WindowManager$BadTokenException:无法添加窗口—— token android.os.BinderProxy@bc0afcf 无效;您的 Activity 正在运行吗? 在 android.view.ViewRootImpl.setView(ViewRootImpl.java:678) 在 android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342) 在 android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93) 在 android.app.Dialog.show(Dialog.java:316) 在 com.thethirdwheelproject.thirdwheel.MainActivity$BackgroundTask.onPostExecute(MainActivity.java:489) 在 com.thethirdwheelproject.thirdwheel.MainActivity$BackgroundTask.onPostExecute(MainActivity.java:389) 在 android.os.AsyncTask.finish(AsyncTask.java:660) 在 android.os.AsyncTask.-wrap1(AsyncTask.java) 在 android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread.java:6077) 在 java.lang.reflect.Method.invoke( native 方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)11-10 08:40:23.733 1334-1334/? E/EGL_emulation:tid 1334:eglCreateSyncKHR(1901):错误0x3004(EGL_BAD_ATTRIBUTE)

最佳答案

Your context variable is not initialized

删除您的 context 变量并使用 MainActivity.this

试试这个方法

@Override
public void onClick(View view) {
view.setEnabled(false);
tvscancontent = tvScanContent.getText().toString();
tvscanformat = tvScanFormat.getText().toString();
BackgroundTask backgroundTask = new BackgroundTask();
backgroundTask.execute(tvscancontent,tvscanformat);
showShowView2();
// displaynotification();

}

});

关于java - 我如何应用alertDialog作为AsyncTask中的服务器响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47217609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com