0

我正在使用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();
}
4

5 回答 5

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 文件。通常我只是把它们放在我正在使用的类文件的底部。

于 2012-04-25T12:46:49.230 回答
0

您正在使用上传代码阻止 UI。您需要使用 AsyncTask / 线程 / 服务。

无痛穿线

于 2012-04-25T12:40:22.853 回答
0

首先,您调用 ProgressDialog.show() 两次,这是不必要的。也许它不显示的一个原因是因为它在显示后立即被解雇。例如,如果您的文件上传速度非常快或失败或在另一个线程中完成,那么对dismiss() 的调用将被调用得如此之快,以至于您将永远看不到对话框。

于 2012-04-25T12:46:03.637 回答
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);
    }
}
于 2012-04-25T12:47:54.613 回答
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();
        }
    }
}
于 2012-04-25T12:48:00.243 回答