非工作重复POST请求

时间:2013-03-05 19:45:00

标签: android android-activity http-post

我的应用中有两项活动。在第一个活动中,我有两个按钮。使用第一个按钮,我发送一个POST请求,其中包含我从TextBoxes获得的数据,使用第二个按钮,我只是跳过此步骤(TextBoxes为空)。这将我带到第二个屏幕,我有另一个按钮,将我发送回第一个屏幕。当发送回第一个屏幕时,我需要填写所有TextBoxes,然后在单击第一个按钮时通过POST请求发送它们的值。但是,我无法发送此请求,因为此应用程序已暂停此步骤。我在LogCat和应用程序本身都没有错误。

可能有什么问题?为什么我的应用程序挂了?

UPD 第一项活动:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first_screen);

    btn_skip = (Button) findViewById(R.id.btn_skip);
    btn_skip.setBackgroundColor(Color.RED);
    btn_skip.setOnClickListener(this);

    btn_get_access = (Button) findViewById(R.id.btn_getAccess);
    btn_get_access.setBackgroundColor(Color.GREEN);

    //set email-error color
    email_error = (TextView) findViewById(R.id.email_error);
    email_error.setTextColor(Color.RED);
    email_error.setVisibility(View.GONE);
    email_error.setTypeface(null,Typeface.BOLD);
    email_confirm_error = (TextView) findViewById(R.id.match_error);
    email_confirm_error.setTextColor(Color.RED);
    email_confirm_error.setVisibility(View.GONE);
    email_confirm_error.setTypeface(null,Typeface.BOLD);

    // check for internet connection
    if (!isOnline()) {
        showNoConnectionDialog(this);
    }
}

@Override
public void onClick(View v) {

    btn_skip = (Button) findViewById(R.id.btn_skip);

    //get IMEI
    TelephonyManager tManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
    imei = tManager.getDeviceId();

    // get data
    if (v == btn_skip && email_txt == null && confirme_email == null)
    {
        email_txt = "example@example.com";
        confirme_email = "example@example.com";
        sendPostRequest(email_txt, confirme_email, imei);
    }
    else if (email_txt != null && confirme_email != null)
    {
        email_txt = email.getEditableText().toString();
        confirme_email = confirm_email.getEditableText().toString();

        //if email is not valid then show errors
        if (!checkEmail(email.getText().toString()) && v == btn_get_access)
        {
            email_error.setVisibility(View.VISIBLE);
            email.requestFocus();
            //Toast.makeText(getBaseContext(), "lkdjfgkdjg", Toast.LENGTH_LONG).show();
        }
        else if (!email_txt.equals(confirme_email))
        {
            email_confirm_error.setVisibility(View.VISIBLE);
            confirm_email.requestFocus();
        }
        else 
        {
            email_error.setVisibility(View.GONE);
            email_confirm_error.setVisibility(View.GONE);
            sendPostRequest(email_txt, confirme_email, imei);
        }
    }
}

/****** SEND POST REQUEST ******/
private void sendPostRequest(String email_txt, String confirme_email, String imei) {
    class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {

            // insert data into massiv
            String paramEmail1 = params[0];
            String paramEmail2 = params[1];
            String paramIMEI = params[2];

            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(post_url);

            // send parametrs with values
            BasicNameValuePair email1BasicNameValuePair = new BasicNameValuePair(
                    "email1", paramEmail1);
            BasicNameValuePair email2BasicNameValuePair = new BasicNameValuePair(
                    "email2", paramEmail2);
            BasicNameValuePair IMEIBasicNameValuePair = new BasicNameValuePair(
                            "imei", paramIMEI);

            // insert parametrs into massiv
            List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
            nameValuePairList.add(email1BasicNameValuePair);
            nameValuePairList.add(email2BasicNameValuePair);
            nameValuePairList.add(IMEIBasicNameValuePair);

            try {
                UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(
                        nameValuePairList);
                httpPost.setEntity(urlEncodedFormEntity);

                try {
                    HttpResponse httpResponse = httpClient
                            .execute(httpPost);
                    InputStream inputStream = httpResponse.getEntity()
                            .getContent();
                    InputStreamReader inputStreamReader = new InputStreamReader(
                            inputStream);
                    BufferedReader buffereReader = new BufferedReader(
                            inputStreamReader);
                    StringBuilder stringBuilder = new StringBuilder();

                    String bufferedStrChunk = null;

                    while ((bufferedStrChunk = buffereReader.readLine()) != null) {
                        stringBuilder.append(bufferedStrChunk);
                    }

                    return stringBuilder.toString();
                } catch (ClientProtocolException cpe) {
                    System.out.println("First Exception of HttpResponse: "
                            + cpe);
                    cpe.printStackTrace();
                } catch (IOException ioe) {
                    System.out.println("Second Exception of HttpResponse: "
                            + ioe);
                    ioe.printStackTrace();
                }
            } catch (UnsupportedEncodingException uee) {
                System.out
                        .println("An Exception given  because of UrlEncodedFormEntity argument : "
                                + uee);
                uee.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // Toast.makeText(getApplicationContext(),
            // "POST request has been send sucessfully",
            // Toast.LENGTH_LONG).show();

            super.onPostExecute(result);

            //go to Second Screen after POST
            Intent intent = new Intent(FirstScreen.this, SecondScreen.class);
            startActivity(intent);
        }
    }
    /***** second check of Internet connection *****/
    if (!isOnline()) {
        showNoConnectionDialog(this);
        // Toast.makeText(getApplicationContext(),
        // "Internet connection is disabled!", Toast.LENGTH_LONG).show();
    } else {
        SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
        sendPostReqAsyncTask.execute(email_txt, confirme_email, imei);
    }
}

这是第二个活动:

@Override
public void onClick(View v) 
{
    try
    {
        if (v == back)
        {           
            Intent intent = new Intent(SecondScreen.this, FirstScreen.class);
            startActivity(intent);  
        }
        else
        {       
            String stringClientID = clientID.getEditableText().toString();
            String stringClientPWD = clientPWD.getEditableText().toString();

            //save data
            Editor e = this.getPreferences(Context.MODE_PRIVATE).edit();
            e.putString("ID", stringClientID);
            e.putString("password", stringClientPWD);
            e.commit();

            String getUserID = this.getPreferences(Context.MODE_PRIVATE).getString("ID", null);
            String getUserPWD = this.getPreferences(Context.MODE_PRIVATE).getString("password", null);
        }
    }
    catch(Exception ex)
    {
        Toast.makeText(getBaseContext(), "Error = " + ex.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

0 个答案:

没有答案