我正在使用http post将文件上传到我的服务器我有一个简单的方法来完成工作,但是加载...对话框永远不会显示如何解决这个问题...这是我的代码
public void send_data() throws IOException
{
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
ProgressDialog dialog = ProgressDialog.show(CaptureTestActivity.this, "",
"Loading. Please wait...", true);
dialog.show();
String urlServer = "http://poi.gps.ro:80/postimg?lat=" + String.valueOf(mDraw.lat) + "&lon=" + String.valueOf(mDraw.lon)+"&comment=" + tmp;
Log.w("DHA", urlServer);
URL url = new URL(urlServer);
if (url != null)
{
Log.w("DHA", "Merge aici");
connection = (HttpURLConnection) url.openConnection();
if (connection != null)
{
Log.w("DHA", "Si aici mere!");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Host", "poi.gps.ro");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=*****");
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + "PICT0000" +"\"" + lineEnd);
outputStream.writeBytes(lineEnd);
outputStream.write(btarr.toByteArray());
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
Log.w("DHA", "Incep trimiterea pozei!");
outputStream.flush();
outputStream.close();
int serverResponseCode = connection.getResponseCode();
String conn = connection.getResponseMessage();
// InputStream in = connection.getInputStream();
// StringWriter writer = new StringWriter();
Log.w("DHA", conn);
Log.w("DHA", "Serverul a raspuns cu " + String.valueOf(serverResponseCode));
if (serverResponseCode == 200)
{
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Super :)");
alertDialog.setMessage("Poza a fost trimisa cu success.");
alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
} });
alertDialog.show();
}
}
}
dialog.dismiss();
}
答案 0 :(得分:1)
正如大卫所说,你需要一个AsynTask,最实用的选择。您可以使用 Void,Void,Void 部分做一些聪明的事情,但除非您想查看文档,否则这应该可以正常工作。
public class MyActivity extends Activity {
....
private final class AsyncSender extends AsyncTask<Void, Void, Void> {
ProgressDialog pd;
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(MyActivity.this);
pd.setTitle("Sending Data");
pd.setMessage("Please wait, data is sending");
pd.setCancelable(false);
pd.setIndeterminate(true);
pd.show();
}
@Override
protected Void doInBackground(Void... params) {
send_data(); // You probably have to try/catch this
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
pd.dismiss();
}
}
}
然后当你打电话时,最干净的方法是打电话
new AsyncSender().execute();
另外,我应该提一下,这应该是一个内部类,这意味着不要为它创建一个单独的java文件。通常我只是将它们粘贴在我正在使用的类文件的底部。
答案 1 :(得分:0)
您使用上传代码阻止了用户界面。您需要使用AsyncTask / thread / service。
答案 2 :(得分:0)
首先,您调用ProgressDialog.show()两次,这是不必要的。也许它没有表现出来的一个原因是因为它在展示之后就被解雇了。 例如,如果您的文件上传速度非常快或失败,或者在另一个线程中完成,则会调用dismiss(),以便您无法看到对话框。
答案 3 :(得分:0)
如果您想在后台执行任何任务并希望在屏幕上显示其他内容,则应使用 AsyncTask ...
这是使用AsyncTask登录的一个例子,现在你可以更新它......
public class mytask extends AsyncTask<Object, Object, Object> {
String METHOD_NAME = "Login";
String NAMESPACE = "http://tempuri.org/";
SoapObject request;
String SOAP_ACTION = "http://tempuri.org/Login";
@Override
protected Object doInBackground(Object... params) {
Log.d(TAG, "doInBackground(Void... params");
Object flag = null;
try {
flag = TAGS.sendToServerAndGetResponse(request, SOAP_ACTION);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
return flag;
}
@Override
protected void onPostExecute(Object result) {
if (result.toString().equalsIgnoreCase("true")) {
mIntent = new Intent();
mIntent.setClass(PresenTABActivity.this,
presentation_list.class);
startActivity(mIntent);
finish();
} else {
Toast.makeText(getApplicationContext(),
"Please Enter Valid Username or Password",
Toast.LENGTH_SHORT).show();
edtPass.setVisibility(View.VISIBLE);
edtUname.setVisibility(View.VISIBLE);
chk_remeber_me.setVisibility(View.VISIBLE);
btnSubmit.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.INVISIBLE);
loading.setVisibility(View.INVISIBLE);
}
}
@Override
protected void onPreExecute() {
Log.d(TAG, "onPreExecute()");
request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("Username", edtUname.getText().toString()
.trim());
request.addProperty("password", edtPass.getText().toString().trim());
mProgressBar = (ProgressBar) findViewById(R.id.progress_login);
loading = (TextView) findViewById(R.id.tv_loading);
loading.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.VISIBLE);
edtPass.setVisibility(View.INVISIBLE);
edtUname.setVisibility(View.INVISIBLE);
chk_remeber_me.setVisibility(View.INVISIBLE);
}
}
答案 4 :(得分:0)
查看此页面:http://www.vogella.com/articles/AndroidPerformance/article.html
实施例
private class UploadingTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog mProgressDialog;
@Override
protected void onPreExecute() {
mProgressDialog = ProgressDialog.show(this, title, message, true);
}
@Override
protected Void doInBackground(final Void... params) {
// Do your work .. uploading
return null;
}
@Override
protected void onPostExecute(final Void result) {
if (mProgressDialog != null) {
mProgressDialog.dismiss();
}
}
}