2

疑似ランダム文字列をサーバー データベースに送信する単純なアプリケーションを開発しています。ここでは、文字列をサーバーに送信するために AsyncTask を使用し、フォアグラウンドで ProgressDialog を使用しています。問題は、進行状況ダイアログが停止せず、サーバーで文字列を取得しないことです。srcetring を送信するコードに問題がある可能性があります。私は Android を初めて使用し、インターネットで入手できる情報源から学びます。これが私が使用するコードです。

public class MainActivity extends Activity {

    Button btn;
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView)findViewById(R.id.textView2);
        btn = (Button)findViewById(R.id.button1);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void getThis(View v) {
        String str = "35" +
            Build.BOARD.length()%10+ Build.BRAND.length()%10 + 
            Build.CPU_ABI.length()%10 + Build.DEVICE.length()%10 + 
            Build.DISPLAY.length()%10 + Build.HOST.length()%10 + 
            Build.ID.length()%10 + Build.MANUFACTURER.length()%10 + 
            Build.MODEL.length()%10 + Build.PRODUCT.length()%10 + 
            Build.TAGS.length()%10 + Build.TYPE.length()%10 + 
            Build.USER.length()%10 ; 
        tv.setText(str);
        UploadUniqueID uni=new UploadUniqueID(this,str);
        uni.execute(str);
    }

    class UploadUniqueID extends AsyncTask<String, Integer, String> {

        Context context;
        MainActivity ma;
        ProgressDialog dialog;
        String id;
        public UploadUniqueID(MainActivity activity,String str) {
            ma = activity;
            context = activity;
            dialog = new ProgressDialog(context);
            id = str;
        }

        protected void onPreExecute() {
            this.dialog.setMessage("Progress start");
            this.dialog.setCancelable(true); 
            this.dialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            // perform long running operation operation
            String id=params[0];
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://10.0.2.2/UniqueIdApp/myPHP.php");

            try {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
                nameValuePairs.add(new BasicNameValuePair("android",id));     
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                httpclient.execute(httppost);          
            } catch (Exception e) {
                Log.i("HTTP Failed", e.toString());
            }            

            return null;
        }

        protected void onProgressUpdate(Integer...integers) {

        }

        protected void onPostExecute(String...strings) {
            tv.setText("Sent"); 
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }
}
4

3 に答える 3

0

ProgressDialog dialogメソッドでオブジェクトを初期化し、protected void onCreate(Bundle savedInstanceState)メソッドで進行状況バーを閉じることをお勧めしますonPostExecute(String...strings)。これは乾杯に役立つかもしれません:)。

編集済み

public class MainActivity extends Activity {
ProgressDialog dialog;
Button btn;
TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView)findViewById(R.id.textView2);
    btn = (Button)findViewById(R.id.button1);
    dialog = new ProgressDialog(MainActivity.this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public void getThis(View v) {
    String str = "35" +
        Build.BOARD.length()%10+ Build.BRAND.length()%10 + 
        Build.CPU_ABI.length()%10 + Build.DEVICE.length()%10 + 
        Build.DISPLAY.length()%10 + Build.HOST.length()%10 + 
        Build.ID.length()%10 + Build.MANUFACTURER.length()%10 + 
        Build.MODEL.length()%10 + Build.PRODUCT.length()%10 + 
        Build.TAGS.length()%10 + Build.TYPE.length()%10 + 
        Build.USER.length()%10 ; 
    tv.setText(str);
    UploadUniqueID uni=new UploadUniqueID(this,str);
    uni.execute(str);
}

class UploadUniqueID extends AsyncTask<String, Integer, String> {

    Context context;
    MainActivity ma;
    ProgressDialog dialog;
    String id;
    /*public UploadUniqueID(MainActivity activity,String str) {
        ma = activity;
        context = activity;
        dialog = new ProgressDialog(context);
        id = str;
    }*/

    protected void onPreExecute() {
        dialog.setMessage("Progress start");
        dialog.setCancelable(true); 
        dialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        // perform long running operation operation
        String id=params[0];
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://10.0.2.2/UniqueIdApp/myPHP.php");

        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("android",id));     
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            httpclient.execute(httppost);          
        } catch (Exception e) {
            Log.i("HTTP Failed", e.toString());
        }            

        return null;
    }

    protected void onProgressUpdate(Integer...integers) {

    }

    protected void onPostExecute(String...strings) {
        tv.setText("Sent"); 
        try{
    if(dialog!=null && dialog.isShowing()){
        dialog.setMessage("DownLoading finished");
        dialog.dismiss();
    }
    }catch(Exception e){
        e.printStackTrace();
    }
    }
  }
}
于 2013-01-17T04:34:55.687 に答える
0

UploadUniqueID次のようにして、 AsyncTask クラスでダイアログを作成して初期化することもできます。

private final ProgressDialog dialog = new ProgressDialog(MainActivity.this);

onPostExecute(...)現在行っているように、メソッドでそれを却下するだけです。また、@OverrideアノテーションをonPreExecute()メソッドに追加します。

于 2013-01-17T04:49:25.697 に答える
0

以下のコードを試してください。

public class MainActivity extends Activity {
    Button btn;
    TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.textView1);
        btn = (Button) findViewById(R.id.button1);
    }

    public void getThis(View v) {
        String str = "35" + Build.BOARD.length() % 10 + Build.BRAND.length()
            % 10 + Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10
            + Build.DISPLAY.length() % 10 + Build.HOST.length() % 10
            + Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10
            + Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10
            + Build.TAGS.length() % 10 + Build.TYPE.length() % 10
            + Build.USER.length() % 10;
        tv.setText(str);
        UploadUniqueID uni = new UploadUniqueID(this, str);
        uni.execute(str);
    }

    class UploadUniqueID extends AsyncTask<String, Integer, String> {
        Context context;
        MainActivity ma;
        ProgressDialog dialog;
        String id;

        public UploadUniqueID(MainActivity activity, String str) {
            ma = activity;
            context = activity;
            dialog = new ProgressDialog(activity);
            id = str;
        }

        protected void onPreExecute() {
            dialog.setMessage("Progress start");
            dialog.setCancelable(true);
            dialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            // perform long running operation operation
            String id = params[0];
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                "http://10.0.2.2/UniqueIdApp/myPHP.php");
            try {
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                    1);
                nameValuePairs.add(new BasicNameValuePair("android", id));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                httpclient.execute(httppost);
            } catch (Exception e) {
                Log.i("HTTP Failed", e.toString());
            }

            return null;
        }

        protected void onProgressUpdate(Integer... integers) { }

        protected void onPostExecute(String p_result) {
            super.onPostExecute(p_result);          
            tv.setText("Sent");
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }   
}
于 2013-01-17T04:49:37.630 に答える