0
我試圖使用asynctask上傳實體,如Google的教程Mobile Assistant https://cloud.google.com/developers/articles/how-to-build-mobile-app-with-app-engine-backend-tutorial/。當我將下面的代碼放在MainActivity中時,它可以工作,但如果我嘗試在另一個活動(在本例中爲MakeDesire.java)中使用代碼,則它不起作用。問題是在新類中實現AsyncTask
1)new DesireTask()。execute().......... DesireTask被加下劃線並表示創建新的類。 2)Eclipse不允許私人類DesireTask().....當單擊下劃線「本地類DesireTask的非法修飾符;只允許抽象或最終」但這在MainActivity中起作用。
任何幫助appriciated。提前致謝。
package com.google.samplesolutions.mobileassistant;
import java.io.IOException;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.samplesolutions.mobileassistant.desireendpoint.Desireendpoint;
import com.google.samplesolutions.mobileassistant.desireendpoint.model.Desire;
public class MakeDesire extends Activity{
private OnTouchListener sendexit = null;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.sss);
Button button1 = (Button) findViewById(R.id.button1);
EditText kategori = (EditText) findViewById(R.id.editText1);
final String ktgri = kategori.getText().toString();
EditText mal =(EditText) findViewById(R.id.editText2);
final String malstring = mal.getText().toString();
final CheckBox alrm = (CheckBox) findViewById(R.id.checkBox1);
sendexit = new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event){
if(event.getAction()== MotionEvent.ACTION_UP){
new DesireTask().execute();
finish();
return true;
}
return false;
}
};
button1.setOnTouchListener(sendexit);
class DesireTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
Desire desire = new com.google.samplesolutions.mobileassistant.desireendpoint.model.Desire();
desire.setCtgry(ktgri);
desire.setGood(malstring);
desire.setAlarm(alrm.isChecked());
Desireendpoint.Builder builder = new Desireendpoint.Builder(AndroidHttp.newCompatibleTransport(), new JacksonFactory(), null);
builder = CloudEndpointUtils.updateBuilder(builder);
Desireendpoint endpoint = builder.build();
try {
endpoint.insertDesire(desire).execute();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
}
}