在我的应用程序中它有三个片段。每个片段都有付款按钮,每当我们点击付款按钮时,一个服务就会调用。因此,对于所有片段,我在非活动类中编写了服务。该服务需要来自每个片段的两个变量。所以我将这些变量发送到非活动类,如下所示:
fragment1:
paymentbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Payment payment = new Payment(getActivity(),Mobilenumber,amount);
}
fragment2:
paymentbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Payment payment = new Payment(getActivity(),Mobilenumber,amount);
}
fragment3:
paymentbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Payment payment = new Payment(getActivity(),Mobilenumber,amount);
}
payment.java:
public class Payment {
JSONArray OrderItem;
String mobNumber, amount;
Context context;
String payjson, jsontopayment;
static Payment INSTANCE;
public Payment(Context context, String mobNumber, String amount) {
db = new DatabaseHandler(context);
INSTANCE = this;
this.mobNumber = mobNumber;
this.amount = amount;
new GetPayment().execute(WebUrl.RechargeServiceURL + "AddRecharge");
}
public class GetPayment extends AsyncTask<String, Void, String> {
/*String mJourneyDate;
public GetData(String pJourneyDate) {
this.mJourneyDate = pJourneyDate;
}*/
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String response;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(params[0]);
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(7);
nameValuePair.add(new BasicNameValuePair("UserUniqueID", "823166FC-83B5-4D7A-816BF81B6B0A5177"));
nameValuePair.add(new BasicNameValuePair("AccountNumber", mobNumber));
nameValuePair.add(new BasicNameValuePair("RCTypeID", rctypeid));
nameValuePair.add(new BasicNameValuePair("OperatorID", opid));
nameValuePair.add(new BasicNameValuePair("ServiceTypeID", srid));
nameValuePair.add(new BasicNameValuePair("CircleID", crid));
nameValuePair.add(new BasicNameValuePair("Amount", amount));
nameValuePair.add(new BasicNameValuePair("DeviceID", "yeryer"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
HttpResponse responce = httpclient.execute(httppost);
HttpEntity httpEntity = responce.getEntity();
response = EntityUtils.toString(httpEntity);
// Log.d("response is", response);
return response;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
String Status;
super.onPostExecute(result);
//Log.v("TAG_RESULT",""+result);
if (result != null) {
try {
////here i want to send the result to activty class
//result have json response`enter code here`
} catch (Exception e) {
e.printStackTrace();
}
} else {
Toast.makeText(context, "something went wrong", Toast.LENGTH_LONG).show();
}
}
}
public String getJson() {
payjson = OrderItem.toString();
Log.v("test", "test");
return payjson;
}
}
paymentActivty.java:
public class Payment_Actiivity extends Activity {
Payment pay;
String json;
String str;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment);
// i need the json response in this class
}
}
i got the json response in non activty class. now i need to add this json data to another activity(payment_activity). so how i get my json response data to payment_activty. now my aim is whenever i click payment button in every fragment, it need to go payment_activity. please help me out.
答案 0 :(得分:0)
您可以使用广播来实现您想要的效果。这是代码,试试看:
将此方法添加到要发送结果的位置。
private void sendResult(String result){
Intent intent = new Intent("yourpackage.result");
intent.putExtra("my_result", result);
LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
}
将以下代码添加到您的活动中。
private BroadcastReceiver mReceiver;
// call this method in onCreate()
private void registerResultReceiver(){
mReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// here your get the result
String result = intent.getStringExtra("my_result");
}
};
IntentFilter filter = new IntentFilter("yourpackage.result");
LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver, filter);
}
将此行添加到Activity的onDestroy()方法以取消注册接收器。
LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
答案 1 :(得分:0)
使响应字符串静态全局 公共静态刺痛反应; 像这样添加这一行 response = new GetPayment()。execute(WebUrl.RechargeServiceURL +“AddRecharge”);
并在此解析该响应字符串,如
HttpEntity entity1 = (payment.respone).getEntity();
InputStream stream = entity1.getContent();
r = convertStreamToString(stream);
JSONObject jo = new JSONObject(r);
s= (Integer) jo.get("Flag");
Log.d("json result is:", r);
statusCode = response.getStatusLine().getStatusCode();
} catch (Exception e) {
e.printStackTrace();
Log.d("error", "code"+0);
}
将converttostream方法放入此行为
public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
答案 2 :(得分:0)
有多种方法可以做到这一点,最好的方法之一是编写自己的界面。
public interface MyInterface{
public void getResult(String result);
}
现在在您的Payment类中创建一个接口myInterface的局部变量,并通过更改构造函数来初始化它。
private MyInterface myInterface;
public Payment(Context context,String mobNumber,String amount,MyInterface interface){
db = new DatabaseHandler(context);
INSTANCE=this;
this.mobNumber = mobNumber;
this.amount = amount;
this.myInterface=myInterface
new GetPayment().execute(WebUrl.RechargeServiceURL + "AddRecharge");
}
在onPostExecute()
中调用这种方法 if (result != null) {
try {
myInterface.getResult(result);
} catch (Exception e) {
e.printStackTrace();
}
} else {
Toast.makeText(context, "something went wrong", Toast.LENGTH_LONG).show();
}
现在在您的Activity中,只需实现此接口和getResult()方法。
编辑:
现在对你来说更容易。只需在所有片段中实现MyInterface。
public class Fragment1 extends Fragment implements MyInterface{
//Implemented method
public void getResult(String result){
//access your result from here
}
}