XML code
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.example.dell.fuwuqicunchu.fuwuqicunchu"android:orientation= "Vertical" > <EditText android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Hello world!"Android:id= "@+id/et_1"/> <LinearLayout android:layout_width= "Match_parent"Android:layout_height= "Wrap_content" > <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Volleyget"Android:onclick= "Volleyget"Android:layout_weight= "1"/> <Button android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Volleypost"Android:onclick= "Volleypost"Android:layout_weight= "1"/> </LinearLayout> <EditText android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:id= "@+id/et_2"/></linearlayout>
Java code
PackageCom.example.dell.myfuwuqi;ImportAndroid.app.ProgressDialog;ImportAndroid.os.Bundle;ImportAndroid.os.Message;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.view.View;ImportAndroid.widget.EditText;ImportAndroid.widget.Toast;ImportCom.android.volley.AuthFailureError;Importcom.android.volley.Request;ImportCom.android.volley.RequestQueue;ImportCom.android.volley.Response;ImportCom.android.volley.VolleyError;Importcom.android.volley.toolbox.StringRequest;ImportCom.android.volley.toolbox.Volley;ImportJava.util.HashMap;ImportJava.util.Map; Public classWebactivityextendsappcompatactivity {EditText et_1; EditText et_2; ProgressDialog ProgressDialog; PrivateAndroid.os.Handler Handler =NewAndroid.os.Handler () {@Override Public voidhandlemessage (Message msg) {String content=(String) msg.obj; Et_2.settext (content); //Progressdialog.dismiss (); } }; Requestqueue Requestqueue; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_web); Et_1=(EditText) Findviewbyid (r.id.et_1); Et_1.settext ("Http://192.168.1.101:8080/Testweb/testweb.jsp"); Et_2=(EditText) Findviewbyid (r.id.et_2); Requestqueue=volley.newrequestqueue ( This); } Public voidvolleyget (view view) {NewThread () {@Override Public voidrun () {//ProgressDialog = new ProgressDialog (webactivity.this);stringrequest str=NewStringrequest (Et_1.gettext (). toString (),NewResponse.listener<string>() {@Override Public voidOnresponse (String s) {Message msg=NewMessage (); Msg.obj=s; Handler.sendmessage (msg); } }, NewResponse.errorlistener () {@Override Public voidonerrorresponse (Volleyerror volleyerror) {toast.maketext (webactivity. This, "Failed", Toast.length_short). Show (); Progressdialog.dismiss (); } }); Requestqueue.add (str); }}.start (); } Public voidVolleypost (View v) {NewThread () {@Override Public voidrun () {//Final ProgressDialog Dialog = new ProgressDialog (webactivity.this);stringrequest str=NewStringrequest (Request.Method.POST, Et_1.gettext (). toString (),NewResponse.listener<string>() {@Override Public voidOnresponse (String s) {Message msg=NewMessage (); Msg.obj=s; Handler.sendmessage (msg); } }, NewResponse.errorlistener () {@Override Public voidonerrorresponse (Volleyerror volleyerror) {toast.maketext (webactivity. This, "Failed", Toast.length_short). Show (); //Dialog.dismiss ();}}) {@OverrideprotectedMap<string, string> getparams ()throwsAuthfailureerror {Map<String,String> map =NewHashmap<string,string>(); Map.put ("Name", "Volley"); returnmap; } }; Requestqueue.add (str); Super. Run (); }}.start (); }}
Note:
1, after 4.0, Google forced the connection network can not be accessed in the main thread.
2. Only the main thread (UI thread) can make the UI more explicit.
Use volley to get the Web source on Genymotion