从 EditText 获取文本并在 Android 中使用 HttpPost 发送
Posted
技术标签:
【中文标题】从 EditText 获取文本并在 Android 中使用 HttpPost 发送【英文标题】:Get text from EditText and send with HttpPost in Android 【发布时间】:2016-06-01 21:46:26 【问题描述】: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
现在我将扫描的条形码放入edittext 和textview 中,但我想将结果上传到服务器。我将在下面发布 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
【问题讨论】:
可能重复,***.com/questions/9767952/… @KNeerajLal 不,那部分完全不同,我尝试实现它,但它没有给我所需的解决方案,我只想知道如何从编辑文本中检索数据并将其发送到服务器 您在这样做时遇到了什么问题? 【参考方案1】:考虑使用 volley 进行客户端-服务器通信。它是谷歌推荐的,最重要的是易于使用,并且可以在后台访问服务器。如果您想知道如何使用 volley,请查看此内容。 http://www.androidhive.info/2014/05/android-working-with-volley-library-1/
【讨论】:
以上是关于从 EditText 获取文本并在 Android 中使用 HttpPost 发送的主要内容,如果未能解决你的问题,请参考以下文章
单击按钮时如何在android中获取带有ListView值的复选框,EditText?
Android:BottomSheetDialog中的多行文本EditText