2013-08-29 41 views
1

我已嘗試{}將字符串確認設置爲「已確認付款」。我想setDescription()給String確認。我如何將這個String從void onActivityResult傳遞給公共類EndpointsTask?在Android中傳遞字符串void public

@Override 
protected void onActivityResult (int requestCode, int resultCode, Intent data) { 
    if (resultCode == Activity.RESULT_OK) { 
     PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION); 
     if (confirm != null) { 
      try { 
       Log.i("paymentExample", confirm.toJSONObject().toString(4)); 

       String confirmation = "payment confirmed"; 







      } catch (JSONException e) { 
       Log.e("paymentExample", "an extremely unlikely failure occurred: ", e); 
      } 
     } 
    } 
    else if (resultCode == Activity.RESULT_CANCELED) { 
     Log.i("paymentExample", "The user canceled."); 
    } 
    else if (resultCode == PaymentActivity.RESULT_PAYMENT_INVALID) { 
     Log.i("paymentExample", "An invalid payment was submitted. Please see the docs."); 
    } 
} 


public class EndpointsTask extends AsyncTask<Context, Integer, Long> { 
    protected Long doInBackground(Context... contexts) { 

     Contactinfoendpoint.Builder endpointBuilder = new Contactinfoendpoint.Builder(
      AndroidHttp.newCompatibleTransport(), 
      new JacksonFactory(), 
      new HttpRequestInitializer() { 
      public void initialize(HttpRequest httpRequest) { } 
      }); 
    Contactinfoendpoint endpoint = CloudEndpointUtils.updateBuilder(
    endpointBuilder).build(); 





    try { 




     ContactInfo note = new ContactInfo().setDescription(confirmation); 
     String noteID = new Date().toString(); 
     note.setId(noteID); 

     EditText streetName; 
     streetName = (EditText) findViewById (R.id.streetAddress); 
     String streetInfo = streetName.getText().toString(); 
     note.setStreetAddress(streetInfo); 

     EditText firstNameText; 
     firstNameText = (EditText) findViewById (R.id.firstName); 
     String firstNameInfo = firstNameText.getText().toString(); 
     note.setNameFirst(firstNameInfo); 

     EditText lastNameText; 
     lastNameText = (EditText) findViewById (R.id.lastName); 
     String lastNameInfo = lastNameText.getText().toString(); 
     note.setNameLast(lastNameInfo); 

     EditText emailText; 
     emailText = (EditText) findViewById (R.id.textemail); 
     String emailInfo = emailText.getText().toString(); 
     note.setEmailAddress(emailInfo); 

     EditText zipText; 
     zipText = (EditText) findViewById (R.id.zipCode); 
     String zipInfo = zipText.getText().toString(); 
     note.setZipCode(zipInfo); 

     EditText stateText; 
     stateText = (EditText) findViewById (R.id.state); 
     String stateInfo = stateText.getText().toString(); 
     note.setState(stateInfo); 

     EditText phoneText; 
     phoneText = (EditText) findViewById (R.id.phoneNumber); 
     String phoneInfo = phoneText.getText().toString(); 
     note.setPhone(phoneInfo); 





     ContactInfo result = endpoint.insertContactInfo(note).execute(); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } 
     return (long) 0; 
    } 
} 

EDITED CODE,現在爲什麼每次按下按鈕時我的數據存儲視圖中都會出現兩個條目。

@Override 
protected void onActivityResult (int requestCode, int resultCode, Intent data) { 
    if (resultCode == Activity.RESULT_OK) { 
     PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION); 
     if (confirm != null) { 
      try { 
       Log.i("paymentExample", confirm.toJSONObject().toString(4)); 

       String confirmation = "payment confirmed"; 

       EndpointsTask task = new EndpointsTask(confirmation); 
       task.execute(this); 



       // TODO: send 'confirm' to your server for verification. 
       // see https://developer.paypal.com/webapps/developer/docs/integration/mobile/verify-mobile-payment/ 
       // for more details. 

      } catch (JSONException e) { 
       Log.e("paymentExample", "an extremely unlikely failure occurred: ", e); 
      } 
     } 
    } 
    else if (resultCode == Activity.RESULT_CANCELED) { 
     Log.i("paymentExample", "The user canceled."); 
    } 
    else if (resultCode == PaymentActivity.RESULT_PAYMENT_INVALID) { 
     Log.i("paymentExample", "An invalid payment was submitted. Please see the docs."); 
    } 
} 


public class EndpointsTask extends AsyncTask<Context, Integer, Long> { 

    String confirmation; 
    public EndpointsTask(String confirmation) { 
    this.confirmation = confirmation; 
    } 


    protected Long doInBackground(Context... contexts) { 

     Contactinfoendpoint.Builder endpointBuilder = new Contactinfoendpoint.Builder(
      AndroidHttp.newCompatibleTransport(), 
      new JacksonFactory(), 
      new HttpRequestInitializer() { 
      public void initialize(HttpRequest httpRequest) { } 
      }); 
    Contactinfoendpoint endpoint = CloudEndpointUtils.updateBuilder(
    endpointBuilder).build(); 





    try { 




     ContactInfo note = new ContactInfo().setDescription(confirmation); 
     String noteID = new Date().toString(); 
     note.setId(noteID); 

     EditText streetName; 
     streetName = (EditText) findViewById (R.id.streetAddress); 
     String streetInfo = streetName.getText().toString(); 
     note.setStreetAddress(streetInfo); 

     EditText firstNameText; 
     firstNameText = (EditText) findViewById (R.id.firstName); 
     String firstNameInfo = firstNameText.getText().toString(); 
     note.setNameFirst(firstNameInfo); 

     EditText lastNameText; 
     lastNameText = (EditText) findViewById (R.id.lastName); 
     String lastNameInfo = lastNameText.getText().toString(); 
     note.setNameLast(lastNameInfo); 

     EditText emailText; 
     emailText = (EditText) findViewById (R.id.textemail); 
     String emailInfo = emailText.getText().toString(); 
     note.setEmailAddress(emailInfo); 

     EditText zipText; 
     zipText = (EditText) findViewById (R.id.zipCode); 
     String zipInfo = zipText.getText().toString(); 
     note.setZipCode(zipInfo); 

     EditText stateText; 
     stateText = (EditText) findViewById (R.id.state); 
     String stateInfo = stateText.getText().toString(); 
     note.setState(stateInfo); 

     EditText phoneText; 
     phoneText = (EditText) findViewById (R.id.phoneNumber); 
     String phoneInfo = phoneText.getText().toString(); 
     note.setPhone(phoneInfo); 





     ContactInfo result = endpoint.insertContactInfo(note).execute(); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } 
     return (long) 0; 
    } 
} 

@Override 
public void onDestroy() { 
    stopService(new Intent(this, PayPalService.class)); 
    super.onDestroy(); 
} 

}

+0

是否有一個原因爲什麼你將一組Context傳遞給doInBackground方法? – Darussian

回答

3

你必須創建EndpointsTask的AsyncTask內部構造像這樣:

public class EndpointsTask extends AsyncTask<Context, Integer, Long> { 

    String confirmation; 
    public RouteAsyncRunnerUI(String confirmation) { 
    this.confirmation = confirmation; 
    } 
} 

所以,你可以調用EndpointsTask內現場確認的確認,做你喜歡它。

您調用此構造在onActivityResult類,像這樣:

EndpointsTask task = new EndpointsTask(confirmation); 
task.execute(this); 
+0

感謝您的答案,這樣做可以傳遞我的字符串。我現在似乎在我的應用引擎控制檯中註冊了兩個條目,而不是一個條目。我得到一個contactinfo和「確認付款」,我得到一個與「null」和contactinfo。有任何想法嗎? – johnsonjp34

+0

@ coconuts4eva,我猜你只需要contactinfo和支付確認條目?您是否在傳遞/使用確認字符串之前嘗試檢查null? – linus

+0

我似乎已經在onClick中執行了。我擺脫了這一點,它效果很好。謝謝! – johnsonjp34

1

我不明白了一個道理,爲什麼你逝去的語境中的一個數組中的AsyncTask類doInBackground方法。爲了傳遞一個串入的AsyncTask可以使類具有參數化類型

public class EndpointsTask extends AsyncTask<String, Integer, Long> 

,你可以執行使用

new EndpointsTask().execute("any", "amount", "of", "strings); 

這裏是更多的文檔的任務AsyncTasks