MARS-Android视频教程实例源代码第四季第八集(Http操作)AVD版本BuildSDKAsync01activity_main.xmlRelativeLayoutxmlns:android=:tools=:layout_width=match_parentandroid:layout_height=match_parenttools:context=.MainActivityTextViewandroid:id=@+id/myTextViewIdandroid:layout_width=wrap_contentandroid:layout_height=wrap_contentandroid:text=@string/hello_world/Buttonandroid:id=@+id/buttonIdandroid:layout_width=fill_parentandroid:layout_height=wrap_contentandroid:layout_below=@id/myTextViewIdandroid:text=向baidu发送一次请求//RelativeLayoutMainActivity.javapackagemars.http01;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importorg.apache.http.HttpEntity;importorg.apache.http.HttpResponse;importorg.apache.http.client.ClientProtocolException;importorg.apache.http.client.HttpClient;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.impl.client.DefaultHttpClient;importandroid.os.Bundle;importandroid.app.Activity;importandroid.view.Menu;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.Button;publicclassMainActivityextendsActivity{privateButtonbutton=null;privateHttpResponsehttpResponse=null;privateHttpEntityhttpEntity=null;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button=(Button)findViewById(R.id.buttonId);button.setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewv){//TODOAuto-generatedmethodstubHttpGethttpGet=newHttpGet();HttpClienthttpClient=newDefaultHttpClient();InputStreaminputStream=null;try{httpResponse=httpClient.execute(httpGet);httpEntity=httpResponse.getEntity();inputStream=httpEntity.getContent();BufferedReaderreader=newBufferedReader(newInputStreamReader(inputStream));Stringline=;Stringresult=;while((line=reader.readLine())!=null){result=result+line;}System.out.println(result);}catch(Exceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}finally{try{inputStream.close();}catch(Exceptione){e.printStackTrace();}}}});}@OverridepublicbooleanonCreateOptionsMenu(Menumenu){//Inflatethemenu;thisaddsitemstotheactionbarifitispresent.getMenuInflater().inflate(R.menu.activity_main,menu);returntrue;}}ProgressBarAsyncTask.javapackagemars.async02;importandroid.os.AsyncTask;importandroid.widget.ProgressBar;importandroid.widget.TextView;publicclassProgressBarAsyncTaskextendsAsyncTaskInteger,Integer,String{privateTextViewtextView=null;privateProgressBarprogressBar=null;publicProgressBarAsyncTask(TextViewtextView,ProgressBarprogressBar){//super();this.textView=textView;this.progressBar=progressBar;}@OverrideprotectedStringdoInBackground(Integer...arg0){//TODOAuto-generatedmethodstubNetOperatenetOperate=newNetOperate();inti=0;for(i=10;i=100;i+=10){netOperate.operate();publishProgress(i);}returni+arg0[0].intValue()+;}/*(non-Javadoc)*@seeandroid.os.AsyncTask#onPostExecute(java.lang.Object)*/@OverrideprotectedvoidonPostExecute(Stringresult){//TODOAuto-generatedmethodstub//super.onPostExecute(result);textView.setText(异步操作结束!+result);}/*(non-Javadoc)*@seeandroid.os.AsyncTask#onPreExecute()*/@OverrideprotectedvoidonPreExecute(){//TODOAuto-generatedmethodstub//super.onPreExecute();textView.setText(开始异步操作!);}/*(non-Javadoc)*@seeandroid.os.AsyncTask#onProgressUpdate(Progress[])*/@OverrideprotectedvoidonProgressUpdate(Integer...values){//TODOAuto-generatedmethodstub//super.onProgressUpdate(values);intvalue=values[0];progressBar.setProgress(value);}}Manifest.xml?xmlversion=1.0encoding=utf-8?manifestxmlns:android==mars.http01android:versionCode=1android:versionName=1.0uses-sdkandroid:minSdkVersion=10android:targetSdkVersion=16/uses-permissionandroid:name=android.permission.INTERNET/applicationandroid:allowBackup=trueandroid:icon=@drawable/ic_launcherandroid:label=@string/app_nameandroid:theme=@style/AppThemeactivityandroid:name=mars.http01.MainActivityandroid:label=@string/app_nameintent-filteractionandroid:name=android.intent.action.MAIN/categoryandroid:name=android.intent.category.LAUNCHER//intent-filter/activity/application/manifest