简体   繁体   English

从EditText获取文本并在Android中使用HttpPost发送

[英]Get text from EditText and send with HttpPost in Android

public void onActivityResult(int requestCode, int resultCode, Intent intent) {

    if (requestCode == SCANNER_REQUEST_CODE) {
        // Handle scan intent
        if (resultCode == Activity.RESULT_OK) {
            // Handle successful scan
            String contents = intent.getStringExtra("SCAN_RESULT");
            String formatName = intent.getStringExtra("SCAN_RESULT_FORMAT");
            byte[] rawBytes = intent.getByteArrayExtra("SCAN_RESULT_BYTES");
            int intentOrientation = intent.getIntExtra("SCAN_RESULT_ORIENTATION", Integer.MIN_VALUE);
            Integer orientation = (intentOrientation == Integer.MIN_VALUE) ? null : intentOrientation;
            String errorCorrectionLevel = intent.getStringExtra("SCAN_RESULT_ERROR_CORRECTION_LEVEL");
            mBarcodeEdit.setText(contents + "\n\n" + formatName);
            tvScanResults.setText(contents + "\n\n" + formatName);

        } else if (resultCode == Activity.RESULT_CANCELED) {
            // Handle cancel

        }
    } else {
        // Handle other intents
    }

}

Now I am getting the scanned barcode into edittext as well as in textview, but I want to upload the result to the server. 现在,我将扫描的条形码放入edittext以及textview中,但是我想将结果上传到服务器。 I'll post the AsyncTask module below: 我将在下面发布AsyncTask模块:

        public void send1(View v){
               new Send().execute();

          }
            class Send extends AsyncTask<String, Void,Void > {

 HttpClient httpclient;
 HttpPost httppost;
String msg = mBarcodeEdit.getText().toString().trim(); 
           protected Void doInBackground(String... urls) {
        // TODO Auto-generated method stub
         // get the message from the message text box
//   String msg = mBarcodeEdit.getText().toString().trim();
        //Toast.makeText(getBaseContext(),"u clicked           mee",Toast.LENGTH_SHORT).show();
        // make sure the fields are not empty
       if (msg.length()>0)
        {

            httpclient = new DefaultHttpClient();
            httppost = new HttpPost("http://yourwebsite.com/yourPhpScript.php"); // sever id
         try {
           List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
           nameValuePairs.add(new BasicNameValuePair("id", "12345"));
           nameValuePairs.add(new BasicNameValuePair("message", msg));
           httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
           httpclient.execute(httppost);

         } catch (ClientProtocolException e) {
             // TODO Auto-generated catch block
         } catch (IOException e) {
             // TODO Auto-generated catch block
         }
        }
        else
        {
            // display message if text fields are empty
            //Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
        }
    return null;
        }}

   protected void onProgressUpdate(Void... progress) {

          }

        protected void onPostExecute(Void result) {
            mBarcodeEdit.setText(""); // clear text box
         }
          }

Consider using volley for client-server communication. 考虑使用Volley进行客户端-服务器通信。 It's google recommended and most importantly easy to use and takes care of hitting the server in background. 它是Google推荐的,最重要的是易于使用,并且会在后台访问服务器。 If you want to know how to use volley check this out. 如果您想知道如何使用排球,请查看此信息。 http://www.androidhive.info/2014/05/android-working-with-volley-library-1/ http://www.androidhive.info/2014/05/android-working-with-volley-library-1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM