我正在构建一个聊天应用程序,要在其中发送聊天消息,用户必须付费。对于付款,我正在使用Android的Google应用内结算。
我遇到的问题是,当我在聊天窗口中键入消息并单击发送时,将触发应用内帐单并进行付款。付款成功后,理想情况下,发送的消息应反映在聊天消息窗口中,而不是在聊天键入窗口中。
但是,除非我再次点击“发送”,否则聊天消息将保留在键入窗口中。
下面是聊天消息的代码。我尝试了一些选项,例如将发送请求放置在不同的位置,但是没有用。
关于需要在哪里查找/修复的任何想法。
预先感谢。
public class Askquest extends Fragment implements View.OnClickListener, BillingProcessor.IBillingHandler{
// PRODUCT & SUBSCRIPTION IDS
private static final String PRODUCT_ID = "android.test.purchased";
private static final String SUBSCRIPTION_ID = "com.anjlab.test.iab.subs1";
private static final String LICENSE_KEY = "LICENSE***KEY";
private static final String MERCHANT_ID=null;
private static final String TAG = "Askquest";
private BillingProcessor bp;
private boolean readyToPurchase = false;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;
//private RecyclerView.Adapter MessageChatAdapter;
private MessageChatAdapter messageChatAdapter;
private List<SendQues> messages;
//private String userid;
private ImageView img_send;
private EditText et_message;
public static String getTimeStamp() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(new Date());
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(!BillingProcessor.isIabServiceAvailable(getActivity())) {
showToast("In-app billing service is unavailable, please upgrade Android Market/Play to version >= 3.9.16");
}
bp = new BillingProcessor(getActivity(), LICENSE_KEY, MERCHANT_ID,this);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_askquest, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
/*
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_askquest);
*/
messages = new ArrayList<>();
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
messageChatAdapter = new MessageChatAdapter(messages, getActivity());
recyclerView = view.findViewById(R.id.recycler_chat);
recyclerView.setAdapter(messageChatAdapter);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
SharedPreferences msharedprefs = getActivity().getSharedPreferences("sharedprefdata",MODE_PRIVATE);
SharedPreferences.Editor editor = msharedprefs.edit();
String messageBy = msharedprefs.getString("username", null);
String userid = msharedprefs.getString("userid", null);
int channel_id = 4;
//int user_id = 23;
Log.i("TAG", "****"+messageBy+"****"+userid+"****"+channel_id);
ReqResp reqResp = new ReqResp(channel_id, userid, messageBy);
reqResp.setChannelId(channel_id);
reqResp.setUserid(userid);
reqResp.setUsername(messageBy);
fetchMessages(reqResp);
img_send = (ImageView) view.findViewById(R.id.img_send);
et_message = (EditText) view.findViewById(R.id.et_message);
img_send.setOnClickListener(this);
}
private void showToast(String message) {
Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
}
@OnClick({R.id.img_send})
public void onClick(View view) {
if (!readyToPurchase) {
showToast("Billing not initialized.");
return;
}
//bp.purchase(getActivity(), getActivity().getResources().getString(R.string.prodact_id));
if (view == img_send)
{
bp.purchase(getActivity(),PRODUCT_ID);
}
}
private void fetchMessages(final ReqResp reqResp)
{
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(APIUrl.NEWURL)
.addConverterFactory(GsonConverterFactory.create())
.build();
APIService apiService = retrofit.create(APIService.class);
String token = "Bearer " + AppUtil.getSavedPreferences(getActivity(),Constants.TOKEN);
Call<List<SendQues>> call = apiService.aresponse(reqResp,token);
call.enqueue(new Callback<List<SendQues>>() {
@Override
public void onResponse(Call<List<SendQues>> call, Response<List<SendQues>> response) {
// messages = response.body();
if(response != null) {
List<SendQues> sendQues = response.body();
//Toast.makeText(getActivity(), "Fetching Responses", Toast.LENGTH_SHORT).show();
if (sendQues.size() > 0) {
messages.addAll(sendQues);
messageChatAdapter.notifyDataSetChanged();
//MessageChatAdapter messageChatAdapter = new MessageChatAdapter(sendQues, Askquest.this);
//recyclerView.setAdapter(messageChatAdapter);
recyclerView.smoothScrollToPosition(sendQues.size() - 1);
}
Toast.makeText(getActivity(), "Fetching Responses Success", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<List<SendQues>> call, Throwable t) {
Toast.makeText(getActivity(), "Fetching Responses Failure", Toast.LENGTH_SHORT).show();
}
});
}
private void sendMessage(final SendQues messgSend){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(APIUrl.NEWURL)
.addConverterFactory(GsonConverterFactory.create())
.build();
APIService apiService = retrofit.create(APIService.class);
String token = "Bearer " + AppUtil.getSavedPreferences(getActivity(), Constants.TOKEN);
Call<pQuestionResp> call = apiService.aquestion(messgSend,token);
call.enqueue(new Callback<pQuestionResp>() {
@Override
public void onResponse(Call<pQuestionResp> call, Response<pQuestionResp> response) {
pQuestionResp message = response.body();
if (message != null && message.getQStatus().equalsIgnoreCase("Success")) {
messages.add(messgSend);
messageChatAdapter.notifyItemInserted(messages.size()-1);
recyclerView.smoothScrollToPosition(messages.size()-1);
Toast.makeText(getActivity(), "Message Sent", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "messages got null", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<pQuestionResp> call, Throwable t) {
t.printStackTrace();
Toast.makeText(getActivity(), "Error Sending", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onProductPurchased(@NonNull String productId, @Nullable TransactionDetails details) {
showToast("onProductPurchased: " + productId);
if(bp.consumePurchase(PRODUCT_ID)){
String msg = et_message.getText().toString();
if (et_message.getText().toString().isEmpty()) return;
SendQues message = new SendQues();
SharedPreferences msharedprefs = getActivity().getSharedPreferences("sharedprefdata",MODE_PRIVATE);
SharedPreferences.Editor editor = msharedprefs.edit();
String message_by = msharedprefs.getString("username", null);
String userid = msharedprefs.getString("userid",null);
String username = msharedprefs.getString("username",null);
int channel_id = 4;
message.setUserId(userid);
message.setMessageBy(message_by);
message.setMessageAt(String.valueOf(getTimeStamp()));
message.setMessageBody(msg);
message.setId(channel_id);
et_message.setText("");
sendMessage(message);
}else{
Toast.makeText(getActivity(),"not consumed",Toast.LENGTH_LONG).show();
}
}
@Override
public void onPurchaseHistoryRestored() {
showToast("onPurchaseHistoryRestored");
for(String sku : bp.listOwnedProducts())
Log.d(TAG, "Owned Managed Product: " + sku);
for(String sku : bp.listOwnedSubscriptions())
Log.d(TAG, "Owned Subscription: " + sku);
if(bp.consumePurchase(PRODUCT_ID)){
String msg = et_message.getText().toString();
if (et_message.getText().toString().isEmpty()) return;
SendQues message = new SendQues();
SharedPreferences msharedprefs = getActivity().getSharedPreferences("sharedprefdata",MODE_PRIVATE);
SharedPreferences.Editor editor = msharedprefs.edit();
String message_by = msharedprefs.getString("username", null);
String userid = msharedprefs.getString("userid",null);
//int user_id = 23;
int channel_id = 4;
message.setUserId(userid);
message.setMessageBy(message_by);
message.setMessageAt(String.valueOf(getTimeStamp()));
message.setMessageBody(msg);
message.setId(channel_id);
//messages.add(message);
et_message.setText("");
sendMessage(message);
}else{
// bp.purchase(getActivity(),PRODUCT_ID);
Toast.makeText(getActivity(),"not consumed",Toast.LENGTH_LONG).show();
}
}
@Override
public void onBillingError(int errorCode, @Nullable Throwable error) {
showToast("onBillingError: " + Integer.toString(errorCode));
}
@Override
public void onBillingInitialized() {
showToast("onBillingInitialized");
readyToPurchase = true;
}
public void onPointerCaptureChanged(boolean hasCapture) {
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (!bp.handleActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
@Override
public void onDestroy() {
if (bp != null) {
bp.release();
}
super.onDestroy();
}
}