当我按下按钮时,它应该启动asynctask并获得json响应。我已经提供了我的java类代码和异步类代码。
此代码的行为与此类似,
当我输入优惠券代码(正确的优惠券代码)并按下按钮然后它进入捕获状态并打印异常时。当我再次按下回车按钮时,它表示成功(响应200)。之后,如果我输入无效代码并按下按钮,它仍然会显示有效代码。如果我再次使用无效代码按下按钮,则表示无效代码。
我的onclick
功能有什么问题?我真的很困惑。
java class
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cart_activity);
couponBTN.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
String CouponCode = couponET.getText().toString();
new CouponAsyncTask(getBaseContext(), new OnTaskCompletedObject() {
@Override
public void onTaskCompletedObject(JSONObject responseJson) {
Constants.couponDetails = responseJson.toString();
}
}).execute(CouponCode);
loadCouponData();
if (!Utility.isNotNull(CouponCode)) {
Toast.makeText(getApplicationContext(),
"Insert coupon code", Toast.LENGTH_LONG).show();
} else if (status.equals("200")) {
Toast.makeText(getApplicationContext(),
"valid coupon code, processing...",
Toast.LENGTH_LONG).show();
System.out.println("valid200");
} else {
Toast.makeText(getApplicationContext(),
"Invalid coupon code", Toast.LENGTH_LONG)
.show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void loadCouponData() {
String dataCoupon = Constants.getCouponDetails();
try {
JSONObject dataObject = new JSONObject(dataCoupon);
status = dataObject.optString("Status");
discount = dataObject.optString("Discount");
} catch (JSONException e) {
e.printStackTrace();
}
}
asynctask class
public class CouponAsyncTask extends AsyncTask<String, Integer, JSONObject> {
private xxx.OnTaskCompletedObject listener;
private static InputStream stream = null;
private static String API;
private JSONObject responseJson = null;
private Context contxt;
public CouponAsyncTask(Context baseContext, xxx.OnTaskCompletedObject onTaskCompletedObject) {
// API = apiURL;
this.contxt = baseContext;
this.listener = onTaskCompletedObject;
}
// async task to accept string array from context array
@Override
protected JSONObject doInBackground(String... params) {
String path = null;
String response = null;
HashMap<String, String> request = null;
JSONObject requestJson = null;
DefaultHttpClient httpClient = null;
HttpPost httpPost = null;
StringEntity requestString = null;
ResponseHandler<String> responseHandler = null;
Log.i("ItemCode", params[0]);
try {
path = "xxx";
new URL(path);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
// set the API request
request = new HashMap<String, String>();
request.put(new String("CouponCode"), params[0]);
request.entrySet().iterator();
// Store locations in JSON
requestJson = new JSONObject(request);
httpClient = new DefaultHttpClient();
httpPost = new HttpPost(path);
requestString = new StringEntity(requestJson.toString());
// sets the post request as the resulting string
httpPost.setEntity(requestString);
httpPost.setHeader("Content-type", "application/json");
// Handles the response
responseHandler = new BasicResponseHandler();
response = httpClient.execute(httpPost, responseHandler);
responseJson = new JSONObject(response);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
responseJson = new JSONObject(response);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return responseJson;
}
@Override
protected void onPostExecute(JSONObject result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
listener.onTaskCompletedObject(responseJson);
}
}