意图不在第二个活动中接收数据凌空

时间:2015-11-02 11:11:43

标签: android android-intent

嘿开发人员我尝试通过意图发送数据

我发送数据A活动到B活动数据是正确发送A活动但B活动没有收到但是有些数据是接收但是有些数据没有收到

代码是活动

 private void requestForSMS(final String mobile) {
    StringRequest strReq = new StringRequest(Request.Method.POST,
            config.Config.URL_REQUEST_SMS, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d(TAG, response.toString());
            try {
                JSONObject responseObj = new JSONObject(response);
            final  String user = responseObj.getString("uid");
                String message = responseObj.getString("msg");
                Intent intent1 = new Intent(getApplicationContext(),HttpService.class);
                intent1.putExtra("uid", user);      // <---Sending data here this data not recive B Activity ------>
                Log.d("user id going","====>"+user);
               if(!user.equalsIgnoreCase("")){
                    pref.setIsWaitingForSms(true);
                    viewPager.setCurrentItem(1);
                    txtEditMobile.setText(pref.getMobileNumber());
                    layoutEditMobile.setVisibility(View.VISIBLE);

                    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(),
                            "ErrorToast: " + message,
                            Toast.LENGTH_LONG).show();
                }
                // hiding the progress bar
                progressBar.setVisibility(View.GONE);
            } catch (JSONException e) {
                Toast.makeText(getApplicationContext(),
                        "Error: " + e.getMessage(),
                        Toast.LENGTH_LONG).show();
                progressBar.setVisibility(View.GONE);
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "ErrorResponce: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_SHORT).show();
            progressBar.setVisibility(View.GONE);
        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("key","xxxxxxxxxxxxx");
            params.put("mobile", mobile);
            Log.e(TAG, "Posting params: " + params.toString());
            return params;
        }
    };
    int socketTimeout = 60000;
    RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    strReq.setRetryPolicy(policy);

    // Adding request to request queue
  newapp.getInstance().addToRequestQueue(strReq);
}

private void verifyOtp() {
    String otp = inputOtp.getText().toString().trim();
    if (!otp.isEmpty()) {
        Intent grapprIntent = new Intent(getApplicationContext(), HttpService.class);

    // <---- sending data here also B Activity---->
        grapprIntent.putExtra("key","xxxxxxxxxxxx");
        grapprIntent.putExtra("mobileverify", otp);
        startService(grapprIntent);
    } else {
        Toast.makeText(getApplicationContext(), "Please enter the OTP", Toast.LENGTH_SHORT).show();
    }
}
private static boolean isValidPhoneNumber(String mobile) {
    String regEx = "^[0-9]{10}$";
    return mobile.matches(regEx);
}

B活动

public class HttpService extends IntentService {
private static String TAG = HttpService.class.getSimpleName();
public HttpService() {
    super(HttpService.class.getSimpleName());
}
@Override
protected void onHandleIntent(Intent intent) {
    if (intent != null) {
        String otp = intent.getStringExtra("mobileverify");
      final  String user1 = intent.getStringExtra("uid");       //<---- this is not recive value ---->
        verifyOtp(otp,user1);
    }
}
/**
 * Posting the OTP to server and activating the user
 *
 * @param otp otp received in the SMS
 */
private void verifyOtp(final String otp, final String user1){
    StringRequest strReq = new StringRequest(Request.Method.POST,
            config.Config.URL_VERIFY_OTP, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d(TAG, response.toString());
            try {
                JSONObject responseObj = new JSONObject(response);
                // Parsing json object response
                // response will be a json object
                String message = responseObj.getString("msg");
                if (message!="") {
                    // parsing the user profile information
                    JSONObject profileObj = responseObj.getJSONObject(response);
                    String mobile = profileObj.getString("mobile");
                    PrefManager pref = new PrefManager(getApplicationContext());
                    pref.createLogin(mobile);
                    Intent intent = new Intent(HttpService.this, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                    Toast.makeText(getApplicationContext(), "HTTPIF"+message, Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getApplicationContext(), "HTTPELSE"+message, Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                Toast.makeText(getApplicationContext(),
                        "Error: " + e.getMessage(),
                        Toast.LENGTH_LONG).show();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "HTTPError: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }) {
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("key","xxxxxxxxxx");
            params.put("mobileverify", otp);
            params.put("uid",user1);        // here its given error
            Log.e(TAG, "Posting params: " + params.toString());
            return params;
        }
    };
    MyApplication.getInstance().addToRequestQueue(strReq);
}

请帮助我谢谢

2 个答案:

答案 0 :(得分:1)

您的grapprIntent不包含“uid”键的值,因为您没有使用它。你使用了一些intent1,它不再用于任何地方。相反,你需要将“uid”放入grapprIntent

grapprIntent.putExtra("uid", user); 

也许grapprIntent应该是该类的全局变量,或者找到一种在方法之间传递它的方法。

答案 1 :(得分:0)

在Application类中创建一个Global变量,并使用set和get Methods 像这样

<强> Application.class

private String user;
public String setuser(String usermy) {
    this.user = usermy;
    return null;
  } 
 public String getuser()
  {
    return user;
  }

您希望像代码一样发送值设置值

<强> SMSActivity

MyApplication myUser = (MyApplication)getApplicationContext();
        @Override
        public void onResponse(String response) {
            Log.d(TAG, response.toString());
            try {
                JSONObject responseObj = new JSONObject(response);

                String user = responseObj.getString("uid");
                String user1 = myUser.setuser(user);

在HttpService.class中获取值 喜欢这个

MyApplication uidinfo = (MyApplication)getApplicationContext();
        final  String user = uidinfo.getuser();

并在Application标记

中提及manifest.xml
 <application
    android:name=".MyApplication"
      />

快乐编码