我需要在这个类上创建一个Progress对话框。我试图让它显示,而所有方法都完成了。可能吗 ?也许更有经验的人遇到过这个问题?是否可以在此类中执行此操作,或者我需要在从其他活动调用此方法时执行此操作?:
public class Data_synch extends TimerTask {
private Context mContext;
String FormsString;
DatabaseDataHandler db;
static String realLink;
String url = "/android/sync";
User user;
ProgressDialog pd;
protected static final int DIALOG_OK= 0;
ProgressDialog timeBar;
public Data_synch(String realLin, Context context, User user) {
mContext = context;
this.user = user;
// realLink = ((Link_holder)
// mContext.getApplicationContext()).getLink();
realLink = realLin;
url = realLink + url;
// pd = ProgressDialog.show(mContext, "", "Uploading photos", false);
}
@Override
public void run() {
new HttpAsyncTask().execute(url);
// /* sync from sever */
new SyncFromServer(realLink, true, false, false, false,false, user, mContext);
new SyncFromServer(realLink, false, true, false, false,false, user, mContext);
new SyncFromServer(realLink, false, false, true, false, false,user, mContext);
new SyncFromServer(realLink, false, false, false, true, false,user, mContext);
new SyncFromServer(realLink, false, false, false, false, true,user, mContext);
try {
new SyncPhotos(realLink, user, mContext);
} catch (final Exception e) {
System.out.println("SAKIU RAJONAS");
e.printStackTrace();
}
}
public String POST(String x) {
InputStream inputStream = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
// System.out.println("x == " + x);
nameValuePairs
.add(new BasicNameValuePair("email", user.getEmail()));
nameValuePairs.add(new BasicNameValuePair("password", user
.getPassword()));
nameValuePairs.add(new BasicNameValuePair("prefix", user
.getPrefix()));
nameValuePairs.add(new BasicNameValuePair("sync_data", x));
// System.out.println(nameValuePairs);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
HttpResponse httpResponse = httpclient.execute(httpPost);
int statusCode = httpResponse.getStatusLine().getStatusCode();
inputStream = httpResponse.getEntity().getContent();
if (inputStream != null) {
result = convertInputStreamToString(inputStream);
} else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", "FAIL");
}
return result;
}
public static String convertInputStreamToString(InputStream inputStream)
throws IOException {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
String line = "";
String result = "";
while ((line = bufferedReader.readLine()) != null)
result += line;
// System.out.println("result is " + result);
inputStream.close();
return result;
}
public class HttpAsyncTask extends AsyncTask<String, Void, String> {
JSONObject json;
final String is_synch = "1";
String synch;
String damn;
JSONArray JsonObjectsArray = new JSONArray();
public String getdamn() {
return damn;
}
protected String doInBackground(String... urls) {
String log = "nothing";
db = new DatabaseDataHandler(mContext);
List<Data> datas = db.getAllData();
int nr_of_data = 0;
for (Data cn : datas) {
log = cn.getJson();
// synch = cn.getSynch();
// System.out.println(synch);
// if(synch = "0")
// System.out.println(synch);
// if(synch == "0"){
// cn.setSynch("1");
try {
json = new JSONObject(log);
add_JsonObject_to_JSONArray(createJsonObjectString(json, cn));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
nr_of_data++;
}
db.close();
System.out
.println("WAWAWYWAWAWAWYWAWAWAWYWAWAWAWYWAWAWAWYWAWAWAWYWA");
damn = JsonObjectsArray.toString();
return POST(damn);
}
// private JSONArray PostJsonArray(JSONArray x){
// JsonObjectsArray = ;
//
// return x;
// }
//
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
System.out.println(result);
}
private JSONArray add_JsonObject_to_JSONArray(JSONObject jsonobject) {
JsonObjectsArray.put(jsonobject);
return JsonObjectsArray;
}
private JSONObject createJsonObjectString(JSONObject jsonobject, Data cn)
throws JSONException {
String comment = "";
jsonobject.put("comment", comment);
// System.out.println("xxxx : "+ jsonobject);
return jsonobject;
}
}
}