我正在尝试在我的应用程序上接受BrainTree。我不理解异步HTTP客户端的部分。
我该把它放在我的应用程序中的什么地方?目前,我在MainActivity中有它,它给了我'get','TextHttpResponseHandler‘以及'clientToken’的错误。
有什么想法吗?
package com.example.android.payments;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.loopj.android.http.*;
import com.braintreepayments.api.dropin.BraintreePaymentActivity;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
AsyncHttpClient client = new AsyncHttpClient();
client.get("https://your-server/client_token", new TextHttpResponseHandler() {
@Override
public void onSuccess(String clientToken) {
this.clientToken = clientToken;
}
});
public void onBraintreeSubmit(View v) {
Intent intent = new Intent(context, BraintreePaymentActivity.class);
intent.putExtra(BraintreePaymentActivity.EXTRA_CLIENT_TOKEN, clientToken);
// REQUEST_CODE is arbitrary and is only used within this activity.
startActivityForResult(intent, REQUEST_CODE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}发布于 2015-07-08 07:30:30
您对client.get()的调用不在方法体中,因此您的代码在语法上无效。试着把它放在一个方法中(或者构造函数或者初始化块--但是在这两个地方做工作都不是个好主意)。
例如,您可以将该语句放入一个名为fetchClientToken()的新方法中
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) { ... }
AsyncHttpClient client = new AsyncHttpClient();
public void fetchClientToken() {
client.get("https://your-server/client_token", new TextHttpResponseHandler() {
@Override
public void onSuccess(String clientToken) {
this.clientToken = clientToken;
}
});
}
public void onBraintreeSubmit(View v) { ... }
@Override public boolean onCreateOptionsMenu(Menu menu) { ... }
@Override public boolean onOptionsItemSelected(MenuItem item) { ... }
}然后,您需要以某种方式调用fetchClientToken,就像使用任何其他方法一样。
https://stackoverflow.com/questions/31280991
复制相似问题