我正在尝试使用水平进度条,Asynctask但它不适合我.它不显示进度百分比.
在这里我把我的xml和活动文件和屏幕截图文件.

布局xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/progresslayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/imagepeakmedia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/peakmedialogo"
android:layout_marginTop="100sp"
/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imagepeakmedia"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:text="" />
<TextView
android:id="@+id/textpercentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imagepeakmedia"
android:layout_toRightOf="@+id/text"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:layout_marginLeft="5sp"
android:text="%" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/text"
style="?android:attr/progressBarStyleHorizontal"
/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
TestActivity.java
public class TestActivity extends Activity {
ProgressDialog progressdialog;
RelativeLayout progresslayout;
ProgressBar progressbar;
TextView textprogressbar;
TextView textpercentage;
TextView textdownload;
boolean progress=true;
/** Called when the activity is first created. */ …Run Code Online (Sandbox Code Playgroud) 我正在使用Caldroid Lib在我的Android App中创建一个自定义日历,我需要从我的Web服务器加载数据,我有一个asynctask填充HashMap然后我把这些数据放在这样的arraylist中:
class Asistencia extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("http://www.gettford.net/comunidad/api/calendario.php?usuarioID="
+ usuarioID);
if (jsonobject != null && jsonobject.length() > 0) {
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("datos");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON …Run Code Online (Sandbox Code Playgroud) 我已经创建了一个应用程序,其中用户登录他的帐户.为此我使用asyncTask.Everything工作正常,但事情是当我得到响应,我希望进度条停止.但它继续下去.
异步任务
protected void onPreExecute() {
super.onPreExecute ();
CommonFunctions.showProgress (c, "Please Wait", true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute (s);
try {
JSONObject jsonObject = new JSONObject (s.trim ());
JSONObject NewDataSet = jsonObject.getJSONObject ("NewDataSet");
JSONObject Table = NewDataSet.getJSONObject ("Table");
String User_ID = Table.getString ("User_ID");
String Vendor_IEntity_Code = Table.getString ("Vendor_IEntity_Code");
String Vendor_Name = Table.getString ("Vendor_Name");
// setting the preferences
SettingPreference.setUserId (c, User_ID);
SettingPreference.setVendorId (c, Vendor_IEntity_Code);
SettingPreference.setVendorName (c, Vendor_Name);
} catch (JSONException e) {
e.printStackTrace ();
}
CommonFunctions.showProgress (c, "", …Run Code Online (Sandbox Code Playgroud) 我有一个启动屏幕,运行asyncTask从API下载数据.在那个任务上OnPostExecute我运行下一个asyncTask发送存储的电子邮件.一旦完成,我需要AlertDialog弹出一个ok按钮,以便用户知道下载完成.我用这个问题得到了尽可能的:
AsyncTask里面的Android AlertDialog
现在,当我尝试向对话框添加属性时,我收到NullPointerException:
public class JSONParser extends AsyncTask<String, String, JSONObject> {
Context c;
public JSONParser(int api,Context c) {
this.api= api;
this.c = c;
}
...
protected void onPostExecute(JSONObject result) {
JSONObject output = new JSONEmailParser(c).executeOnExecutor(AsyncTask.SERIAL_EXECUTOR, new String[] {null,null,null,null}).get();
}
}
public class JSONEmailParser extends AsyncTask<String, String, JSONObject> {
Context c;
AlertDialog.Builder builder;
public JSONEmailParser(Context c){
this.c = c;
}
protected void onPreExecute(int api){
builder = new AlertDialog.Builder(SplashScreen.this);
}
...
protected void …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用AsyncTask捕获httpclient的响应,但抛出以下错误,验证字符串中的doInBackground是否不兼容:
“返回类型与AsyncTask不兼容”
我尝试捕获httpclient的响应字符串,并在doInBackground中返回它,以便在onPostExecute中使用它。
有人可以帮助我。
谢谢
private String insertar(){
String response = "";
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
HttpPost httppost;
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://10.1.1.20/pruebaAndroid/insert.php"); // Url del Servidor
//Añadimos nuestros datos
nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("dni",dni.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair("nombre",nombre.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair("telefono",telefono.getText().toString().trim()));
nameValuePairs.add(new BasicNameValuePair("email",email.getText().toString().trim()));
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));// envio las variables
// capturo lo que devuelve, si no devolviera nada solo hago la ejecucion del httpclient sin el handler
ResponseHandler<String> responseHandler = new BasicResponseHandler();
response = httpclient.execute(httppost, responseHandler);
//----------------------------------------------------------------
} catch (UnsupportedEncodingException …Run Code Online (Sandbox Code Playgroud) 我创建了这个AsyncTask内部类,现在我想把它变成外层.这是代码:
private class DownloadDataFromServer extends AsyncTask<String, Integer, String> {
ProgressDialog dialog;
boolean connErr = false;
boolean soErr = false;
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(HomePage.this); !!!!PROBLEM!!!!
dialog.setIndeterminate(false);
dialog.setMax(100);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setCancelable(false);
dialog.setTitle("Download");
dialog.setMessage("Updating...");
dialog.show();
super.onPreExecute();
}
@Override
protected String doInBackground(String... urls) {
// do something
}
protected void onProgressUpdate(Integer... progress) {
// Update the progress
dialog.setProgress(progress[0]);
}
@Override
protected void onPostExecute(String result) {
if (connErr || soErr) {
String msg = "Bad connection";
AlertDialog.Builder builder;
builder = new …Run Code Online (Sandbox Code Playgroud) android outer-join inner-classes android-context android-asynctask
我编写了一个类,它启动一个http连接来获取例如网站的文本.我使用AsyncTask但我得到了NetworkOnMainException.你能救我吗?班级
public class getXMLData extends AsyncTask<String, Void, String> {
TextView _textview;
public getXMLData(TextView textview) {
_textview = textview;
}
protected String doInBackground(String... url)
{
String _text = "";
try {
try {
URL _url = new URL(url[0]);
HttpURLConnection con = (HttpURLConnection) _url.openConnection();
_text = readStream(con.getInputStream());
}
catch (Exception e) {
e.printStackTrace();
}
}
catch (Exception e) {
e.printStackTrace();
}
return _text;
}
protected void onPostExecute(String result)
{
_textview.setText(result.toCharArray(), 0, result.length());
}
private String readStream(java.io.InputStream in) {
java.io.BufferedReader reader = null; …Run Code Online (Sandbox Code Playgroud) 我正在使用毕加索来加载图像作为我的活动的背景,我想使用AsyncTask,而图像正在加载,完成后进度条会解散,以便为我的应用程序提供更好的外观,
这是我的代码:
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setMessage("Chargement...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
Picasso.with(MainActivity.this).load("http://tv2.orangeadd.com/mediacenter-data/ofc__bg_home.jpg").into(background,new com.squareup.picasso.Callback() {
@Override
public void onSuccess() {
mProgressDialog.dismiss();
}
@Override
public void onError() {
}
});
return null;
}
@Override
protected void onPostExecute(Void result) {
}
}
Run Code Online (Sandbox Code Playgroud)
这总是显示错误并强制我的应用程序退出!
多谢你们 :)
我有一个异步任务,最终创建参数电子邮件,关于.异步任务位于onCreate方法中,此CustomListAdapter也是如此.我想在asyncTask中的onCreate中移动CustomListAdapter.onPostExecute我的问题是这this意味着应用程序的上下文.当我移动它时,onPostExecute它不再具有上下文.我试图用它替换它getApplicationContext(),activityName.this.getApplicationContext但它不起作用,因为它期望应用程序的上下文.我发现这种静态方式可以在Android上获得"上下文"?但由于某种原因,我无法在清单中定义名称.我可以在asyncTask的onPostExecute方法中使用哪种获取应用程序上下文的方法?或者我应该以上下文作为参数来制作Asynck任务?
CustomListAdapter adapter=new CustomListAdapter(this, email,about);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Slecteditem= email.get(position);
Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();
}
});
Run Code Online (Sandbox Code Playgroud) 我想用rxAndroid重写项目.在我的代码中,我有这个简单的部分:
public void chkLogin(String login, String password) {
String[] myTaskParams = { login, password };
new ChkLoginTask().execute(myTaskParams);
}
private class ChkLoginTask extends AsyncTask<String, Void, LoginStatus> {
@Override
protected void onPreExecute(){
view.showLoadingSpinner();
}
@Override
protected LoginStatus doInBackground(String... params) {
return app.getAPI().checkLogin(params[0], params[1]);
}
@Override
protected void onPostExecute(LoginStatus result) {
view.hideLoadingSpinner();
if(result == null)
view.onConnectionError();
else{
view.onChkLoginSuccess(result);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在这段代码中,我只用两个参数在AsyncTask中调用我的api方法,然后根据它的结果做出反应.
现在我想用rxAndroid编写这段代码,但我坚持下去......我知道rx如何处理对UI元素的反应,但无法找到有关它如何与参数化AsyncTask一起工作的信息
在这种情况下使用RX是否正确?
android ×10
java ×2
hashmap ×1
outer-join ×1
picasso ×1
progress-bar ×1
rx-android ×1
rx-java ×1
string ×1