异步任务实施问题

时间:2013-03-28 16:23:53

标签: java android http post android-asynctask

我不知道我做错了什么 - 但是我在组合来自两个不同文件的源代码后编译以下代码时遇到了一些问题。关于可能导致这些问题的任何建议(作为学习经验)以及如果解决这些问题并不太复杂,我们对此表示赞赏。

(提前致谢!)

JAVA:

import android.app.Activity;
import android.app.AlertDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.ViewGroup;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TimePicker;
import android.widget.Toast;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.opengl.Visibility;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;




public class AddEditCountry extends Activity {

     private long rowID; 
     private EditText nameEt;
     private EditText capEt;
     private EditText codeEt;
     private TimePicker timeEt;


            private EditText value;
            private Button btn;
            private ProgressBar pb;
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.deviceconfig);
                value=(EditText)findViewById(R.id.editText1);
                btn=(Button)findViewById(R.id.button1);
                pb=(ProgressBar)findViewById(R.id.progressBar1);
                pb.setVisibility(View.GONE);
                nameEt = (EditText) findViewById(R.id.nameEdit);
                  capEt = (EditText) findViewById(R.id.capEdit);
                  codeEt = (EditText) findViewById(R.id.codeEdit);
                  timeEt = (TimePicker) findViewById(R.id.timeEdit);




                  Bundle extras = getIntent().getExtras(); 

                  if (extras != null)
                  {
                     rowID = extras.getLong("row_id");
                     nameEt.setText(extras.getString("name"));  
                     capEt.setText(extras.getString("cap"));  
                     codeEt.setText(extras.getString("code"));    
                     String time = extras.getString("time");
                     String[] parts = time.split(":");
                     timeEt.setCurrentHour(Integer.valueOf(parts[0]));
                     timeEt.setCurrentMinute(Integer.valueOf(parts[1]));
                     timeEt.setIs24HourView(false);
                     DecimalFormat df =   new DecimalFormat  ("00");
                     String minutes = df.format(min);
            }
            }

            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
                getMenuInflater().inflate(R.menu.main, menu);
                return true;
            }

            public void onClick(View v) {
                // TODO Auto-generated method stub
                        pb.setVisibility(View.VISIBLE);
                        (value.getText().toString());       
                    }{


            new MyAsyncTask().execute

             class AddEditCountry extends AsyncTask<String, Integer, Double>{

                @Override
                protected Double doInBackground(String... params) {
                    // TODO Auto-generated method stub
                    postData(params[0]);
                    return null;
                }

                protected void onPostExecute(Double result){
                    pb.setVisibility(View.GONE);
                    Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_LONG).show();
                }
                protected void onProgressUpdate(Integer... progress){
                    pb.setProgress(progress[0]);
                }

                public void postData(String valueIWantToSend) {
                    // Create a new HttpClient and Post Header
                    HttpClient httpclient = new DefaultHttpClient();
                    HttpPost httppost = new HttpPost("http://gamedemo.hostzi.com/apply.cgi");

                    try {
                        // Add your data
                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                        nameValuePairs.add(new BasicNameValuePair("submit_button", "Wireless_MAC"));
                        nameValuePairs.add(new BasicNameValuePair("change_action", ""));
                        nameValuePairs.add(new BasicNameValuePair("action", "Apply"));
                        nameValuePairs.add(new BasicNameValuePair("wl_macmode", "allow"));
                        nameValuePairs.add(new BasicNameValuePair("wl_maclist", "32"));
                        nameValuePairs.add(new BasicNameValuePair("wait_time", "3"));
                        nameValuePairs.add(new BasicNameValuePair("wl_mac_filter", "1"));
                        nameValuePairs.add(new BasicNameValuePair("start", "allow"));
                        nameValuePairs.add(new BasicNameValuePair("wl_macmode1", "allow"));
                        nameValuePairs.add(new BasicNameValuePair("m0", "00%3A1E%3A33%3AFE%3A0D%3A38"));
                        nameValuePairs.add(new BasicNameValuePair("m16", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m1", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m17", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m2", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m18", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m3", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m19", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m4", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m20", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m5", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m21", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m6", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m22", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m7", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m23", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m8", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m24", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m9", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m25", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m10", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m26", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m11", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m27", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m12", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m28", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m13", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m29", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m14", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m30", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m15", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("m31", "00%3A00%3A00%3A00%3A00%3A00"));
                        nameValuePairs.add(new BasicNameValuePair("end", ""));

                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                        // Execute HTTP Post Request
                        HttpResponse response = httpclient.execute(httppost);

                    } catch (ClientProtocolException e) {
                        // TODO Auto-generated catch block
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                    }
                }





          Button saveButton =(Button) findViewById(R.id.saveBtn);
          saveButton.setOnClickListener(new OnClickListener() {

              public void onClick(View v) 
              {
                 if (nameEt.getText().length() != 0)
                 {
                    AsyncTask<Object, Object, Object> saveContactTask = 
                       new AsyncTask<Object, Object, Object>() 
                       {
                          @Override
                          protected Double doInBackground(Object... params) 
                          {
                             saveContact();
                             return null;
                          }

                          @Override
                          protected void onPostExecute(Object result) 
                          {
                             finish();
                          }
                       }; 

                    saveContactTask.execute((Object[]) null); 
                 }

                 else
                 {
                    AlertDialog.Builder alert = new AlertDialog.Builder(AddEditCountry.this);
                    alert.setTitle(R.string.errorTitle); 
                    alert.setMessage(R.string.errorMessage);
                    alert.setPositiveButton(R.string.errorButton, null); 
                    alert.show();
                 }
              } 
         });
       }


           private void saveContact() 
           {
              DatabaseConnector dbConnector = new DatabaseConnector(this);

              if (getIntent().getExtras() == null)
              {
                  dbConnector.insertContact(nameEt.getText().toString(),
                          capEt.getText().toString(),
                          timeEt.getCurrentHour().toString() + ":"
                              + timeEt.getCurrentMinute().toString(),
                          codeEt.getText().toString());

              }
              else
              {
                 dbConnector.updateContact(rowID,
                         nameEt.getText().toString(),
                            capEt.getText().toString(),
                              timeEt.getCurrentHour().toString() + ":"
                                  + timeEt.getCurrentMinute().toString(),
                              codeEt.getText().toString());
              }
           }}

问题:

    Description Resource    Path    Location    Type
The left-hand side of an assignment must be a variable  AddEditCountry.java /app/Game Demo/src/com/app/gamedemo line 116    Java Problem
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (AddEditCountry)   AddEditCountry.java /app/Game Demo/src/com/app/gamedemo line 80 Java Problem
Syntax error on token(s), misplaced construct(s)    AddEditCountry.java /app/Game Demo/src/com/app/gamedemo line 60 Java Problem
Syntax error, insert ";" to complete BlockStatements    AddEditCountry.java /app/Game Demo/src/com/app/gamedemo line 120    Java Problem
Syntax error, insert "AssignmentOperator Expression" to complete Assignment AddEditCountry.java /app/Game Demo/src/com/app/gamedemo line 120    Java Problem
Syntax error, insert "AssignmentOperator Expression" to complete Expression AddEditCountry.java /app/Game Demo/src/com/app/gamedemo line 116    Java Problem
Syntax error, insert "AssignmentOperator Expression" to complete Expression AddEditCountry.java /app/Game Demo/src/com/app/gamedemo line 116    Java Problem
Syntax error on token ".", = expected   AddEditCountry.java /app/Game Demo/src/com/app/gamedemo line 1  Java Problem
Syntax error on token(s), misplaced construct(s)    AddEditCountry.java /app/Game Demo/src/com/app/gamedemo line 122    Java Problem
Syntax error on tokens, AnnotationName expected instead AddEditCountry.java /app/Game Demo/src/com/app/gamedemo line 207    Java Problem

2 个答案:

答案 0 :(得分:0)

我认为您在AsyncTask结束时(在onCreate之前)忘记了一个'}'

答案 1 :(得分:0)

从我所看到的,这是你的问题:

  1. 每个nameValuePairs.add(new BasicNameValuePair(.. , ..);最后都需要额外)
  2. 您错过}来结束AsyncTask的范围,我实际上认为您也缺少2来结束您的内部课程(否则您有重复的onCreate方法)
  3. 您的MyAsyncTask班级有public修饰符。删除它。
  4. 您的doInBackground方法应返回Double而不是Object
  5. 您的AsyncTask现在称为AddEditCountry,与您的外层相同。这是不可能的。改变其中一个名字。
  6. 您的AsyncTask类正在 中定义<{1}}类。将类移出方法。 ¨
  7. 您无法在onClick(View v)中使用this,因为点击监听器需要是btn.setOnClickListener(this);,而您的班级不是。{1}}。您可以通过创建一个实现onClick方法的新OnClickListener并将其作为参数传递来解决此问题。 (就像你使用保存按钮一样)
  8. 我不明白为什么你有一个内部的Activity类。我建议你重新考虑一下你是否真的想要内部活动,或者你只想要OnClickListenerDeviceConfig的方法/功能,或者它们应该只是两个不同的文件。

    编辑:列出更多问题。