我有一个小功能的应用程序,它根据按钮点击发送的用户输入向服务器发出httprequest.我正在寻找一些帮助,使我的小应用程序与SDK 11兼容,使这个httprequest成为异步任务.
我花了几天时间阅读有关异步任务的内容,并了解原理以及使httprequest远离UI线程的原因.
但是,我无法让代码在我的情况下工作.我甚至无法编译.我已经包含在我的功能代码下面(即在尝试使httprequest异步之前)
我非常感谢一些具体的帮助.我为我的垃圾代码道歉并且已经回答了这个问题的变化.
提前致谢
杰米
我的MainActivity代码如下:
package com.jrcdesign.ebookbeamer;import java.io.IOException;import org.apache.http.clIEnt.ClIEntProtocolException;import java.util.ArrayList;import java.util.List;import org.apache.http.clIEnt.httpClIEnt;import org.apache.http.clIEnt.methods.httpPost;import org.apache.http.impl.clIEnt.DefaulthttpClIEnt;import org.apache.http.nameValuePair;import org.apache.http.clIEnt.entity.UrlEncodedFormEntity;import org.apache.http.message.BasicnameValuePair;import androID.app.Activity;import androID.os.Bundle;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.Toast;import androID.Widget.Spinner;import androID.vIEw.VIEw.OnClickListener;public class MainActivity extends Activity {button sendbutton;button btnsubmit;EditText msgTextFIEld;EditText msg2TextFIEld;Spinner spinner1;/** Called when the activity is first created. */@OverrIDepublic voID onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); // load the layout setContentVIEw(R.layout.main); // make message text fIEld object msgTextFIEld = (EditText) findVIEwByID(R.ID.msgTextFIEld); msg2TextFIEld = (EditText) findVIEwByID(R.ID.msg2TextFIEld); // make send button object sendbutton = (button) findVIEwByID(R.ID.sendbutton); btnsubmit = (button) findVIEwByID(R.ID.btnsubmit); addListenerOnbutton(); addListenerOnSpinnerItemSelection(); } public voID addListenerOnSpinnerItemSelection() { spinner1 = (Spinner) findVIEwByID(R.ID.spinner1); spinner1.setonItemSelectedListener(new CustomOnItemSelectedListener()); } // get the selected dropdown List value public voID addListenerOnbutton() { spinner1 = (Spinner) findVIEwByID(R.ID.spinner1); btnsubmit = (button) findVIEwByID(R.ID.btnsubmit); btnsubmit.setonClickListener(new OnClickListener() { @OverrIDe public voID onClick(VIEw v) { Toast.makeText(MainActivity.this,"OnClickListener : " + "\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()),Toast.LENGTH_SHORT).show(); msgTextFIEld.setText("" + String.valueOf(spinner1.getSelectedItem())); } }); } // Called when the SEND button is pressed // Need to make this an async task public voID send(VIEw v){ // get the message from the message text Box msgTextFIEld.setText("" + String.valueOf(spinner1.getSelectedItem())); String msg = msgTextFIEld.getText().toString(); String msg2 = msg2TextFIEld.getText().toString(); if (msg.length()>0) { httpClIEnt httpclIEnt = new DefaulthttpClIEnt(); httpPost httppost = new httpPost("http://54.235.198.96/test1.PHP"); try { List<nameValuePair> nameValuePairs = new ArrayList<nameValuePair>(2); nameValuePairs.add(new BasicnameValuePair("ID",msg2)); nameValuePairs.add(new BasicnameValuePair("message",msg)); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); httpclIEnt.execute(httppost); msgTextFIEld.setText(""); // clear text Box msg2TextFIEld.setText(""); // clear text Box Toast.makeText(MainActivity.this,"Your request is being processed",Toast.LENGTH_LONG).show(); } 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 fIElds are required",Toast.LENGTH_SHORT).show(); }}}解决方法 这是您的异步类:
private class AsyncTaskDownloadSomething extends AsyncTask<String[],String,String> { DataClassLentABook mData; @OverrIDe protected voID onPreExecute() { super.onPreExecute(); //Do some prepartations over here,before the task starts to execute //like freeze the button and/or show a progress bar } @OverrIDe protected String doInBackground(String... urls) { // Task starts executing. String url = urls[0]; // Execute http requests here,with one url(urls[0]),// or many urls using the urls table // Save result in myresult return myresult; } protected voID onPostExecute(String result) { //Do modifications you want after everything is finished //like re-enable the button,and/or hIDe a progressbar //And of course do what you want with your result got from http-req }}
要执行异步任务,单击按钮时,只需写下:
new AsyncTaskDownloadSomething().execute(someURL);总结
以上是内存溢出为你收集整理的android – 如何使httprequest成为异步任务的示例全部内容,希望文章能够帮你解决android – 如何使httprequest成为异步任务的示例所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)