【Android源码 栏目提醒】:网学会员为广大网友收集整理了,MARS-android视频教程代码-第四季第九集-HTTP操作 - 其它资料,希望对大家有所帮助!
MARS-Android 视频教程实例源代码第四季第九集(Http 操作)AVD 版本Build SDKactivity_main.xmlMainActivity.javapackage mars.http02import java.io.BufferedReaderimport java.io.IOExceptionimport java.io.InputStreamimport java.io.InputStreamReaderimport java.io.UnsupportedEncodingExceptionimport java.util.ArrayListimport java.util.Listimport org.apache.http.HttpEntityimport org.apache.http.HttpResponseimport org.apache.http.NameValuePairimport org.apache.http.client.ClientProtocolExceptionimport org.apache.http.client.HttpClientimport org.apache.http.client.entity.UrlEncodedFormEntityimport org.apache.http.client.methods.HttpGetimport org.apache.http.client.methods.HttpPostimport org.apache.http.impl.client.DefaultHttpClientimport org.apache.http.message.BasicNameValuePairimport android.os.Bundleimport android.app.Activityimport android.view.Menuimport android.view.Viewimport android.view.View.OnClickListenerimport android.widget.Buttonimport android.widget.EditTextimport android.widget.TextViewpublic class MainActivity extends Activity private Button getButtonnull private Button postButtonnull private EditText nameViewnull private EditText ageViewnull private StringbaseURLhttp://192.168.0.100:8080/serverside/name.jsp private HttpResponse httpResponsenull private HttpEntity httpEntitynull Override protected void onCreateBundle savedInstanceState super.onCreatesavedInstanceState setContentViewR.layout.activity_main nameViewEditTextfindViewByIdR.id.nameViewId ageViewEditTextfindViewByIdR.id.ageViewId getButtonButtonfindViewByIdR.id.getButtonId getButton.setOnClickListenernew OnClickListener Override public void onClickView v // TODO Auto-generated method stub String namenameView.getText.toString String ageageView.getText.toString String urlbaseURLnamenameageage System.out.printlnurl HttpGet httpGetnew HttpGeturl HttpClient httpClientnew DefaultHttpClient InputStream inputStreamnull try httpResponsehttpClient.executehttpGet httpEntityhttpResponse.getEntity inputStreamhttpEntity.getContent BufferedReader readernew BufferedReadernewInputStreamReaderinputStream String result String line whilelinereader.readLinenull resultresultline System.out.printlnresult catch Exception e // TODO Auto-generated catch block e.printStackTrace finally try inputStream.close catch IOException e // TODO Auto-generated catch block e.printStackTrace postButtonButtonfindViewByIdR.id.postButtonId postButton.setOnClickListenernew OnClickListener Override public void onClickView v // TODO Auto-generated method stub String namenameView.getText.toString String ageageView.getText.toString NameValuePair nameValuePair1newBasicNameValuePairname name NameValuePair nameValuePair2newBasicNameValuePairageage List nameValuePairsnewArrayList nameValuePairs.addnameValuePair1 nameValuePairs.addnameValuePair2 try HttpEntity requestHttpEntitynewUrlEncodedFormEntitynameValuePairs HttpPost httpPostnew HttpPostbaseURL httpPost.setEntityrequestHttpEntity HttpClient httpClientnew DefaultHttpClient InputStream inputStreamnull try httpResponsehttpClient.executehttpPost httpEntityhttpResponse.getEntity inputStreamhttpEntity.getContent BufferedReader readernew BufferedReadernewInputStreamReaderinputStream String result String line whilelinereader.readLinenull resultresultline System.out.printlnresult catch Exception e // TODO Auto-generated catch block e.printStackTrace finally try inputStream.close catch IOException e // TODO Auto-generated catch block e.printStackTrace catch Exception e // TODO Auto-generated catch block e.printStackTrace Override public boolean onCreateOptionsMenuMenu menu // Inflate the menu this adds items to the action bar if it ispresent. getMenuInflater.inflateR.menu.activity_main menu return true ProgressBarAsyncTask.javapackage mars.async02import android.os.AsyncTaskimport android.widget.ProgressBarimport android.widget.TextViewpublic class ProgressBarAsyncTask extendsAsyncTask private TextView textViewnull private ProgressBar progressBarnull public ProgressBarAsyncTaskTextView textView ProgressBarprogressBar //super this.textView textView this.progressBar progressBarOverrideprotected String doInBackgroundInteger... arg0 // TODO Auto-generated method stub NetOperate netOperatenew NetOperate int i0 fori10i上一篇:[Android.2.0游戏开发实践宝典》源码Android游戏开发视频教程已更新到16章了].12