Package siwi. map. android; import java. io. bufferedReader; import java. io. IOException; import java. io. inputStreamReader; import java. io. unsupportedEncodingException; import java. util. arrayList; import java. util. list; import org. apache. http. httpEntity; import org. apache. http. httpResponse; import org. apache. http. nameValuePair; import org. apache. http. client. clientProtocolException; import org. apache. http. client. httpClient; import org. apache. http. client. entity. urlEncodedFormEntity; import org. apache. http. client. methods. httpGet; import org. apache. http. client. methods. httpPost; import org. apache. http. impl. client. defaultHttpClient; import org. apache. http. message. basicNameValuePair; import org. apache. http. protocol. HTTP; import org. apache. http. util. entityUtils; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. widget. textView; import android. widget. toast; public class MainActivity extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); TextView response = (TextView) findViewById (R. id. TV); // pass httpGet (response) through get; // pass httpPost () through post;} private void httpPost () {HttpClient hc = new DefaultHttpClient (); httpPost post = new HttpPost (" http://192.168.1.11:8080/foo/secret.jsp "); // If the number of passed parameters is large, you can encapsulate the List <NameValuePair> params = new ArrayList <NameValuePair> (); params. add (new BasicNameValuePair ("name", "zhangsan"); params. add (new BasicNameValuePair ("pass", "123456"); try {post. setEntity (new UrlEncodedFormEntity (params, HTTP. UTF_8); HttpResponse response2 = hc.exe cute (post); // if the server returns response if (response2.getStatusLine () successfully (). getStatusCode () == 200) {String msg = EntityUtils. toString (response2.getEntity (); Toast. makeText (this, msg, 5000 ). show () ;}} catch (UnsupportedEncodingException e) {e. printStackTrace ();} catch (ClientProtocolException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}} private void httpGet (TextView response) {// create the DefaultClient object HttpClient hc = new DefaultHttpClient (); // create an httpGet object HttpGet = new HttpGet (" http://192.168.1.11:8080/foo/secret.jsp "); Try {HttpResponse httpResponse = hc.exe cute (get); HttpEntity entity = httpResponse. getEntity (); if (null! = Entity) {// read Server Response BufferedReader br = new BufferedReader (new InputStreamReader (entity. getContent (); String line = null; response. setText (""); while (null! = (Line = br. readLine () {// use the response text box to display the server response. append (line + "\ n") ;}} catch (ClientProtocolException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
These days, I have been confused about this client, and I have no direction for self-learning. As a result, the progress has not been very large, and there are many factors that may lead to low progress, as a result, my own ideas have never been grasped, my emotions have been affected, and I have no experienced person to guide me. I have no experience in the company, there are too many search results on the internet, no detailed tutorials, or you may want to buy a book for research, but you just don't know which ones are linked to what you want to do now. In short, you still have to do it yourself, so you can only study it slowly!
The above code finds two methods for http requests: httppost and httpget. GET can GET static pages, or put parameters behind the URL string and pass them to the server. The parameters of the POST method are stored in the Http request. When passing parameters using the POST method, you need to use NameValuePair to save the parameters to be passed. In addition, you must set the character set used. Character encoding is required for POST transmission.