Android Network Programming-http sending/request service

Source: Internet
Author: User

Android Network Programming-http sending/request service

 

I recently learned about Android network programming. The following are some of my experiences tonight. Share with you.
In actual application development, the app often needs to request data from the server. How does the app send requests? The following code is a case where HttpURLConnection is used to send the requested data to the server and then process the data returned by the server.

The following code is a simple answer test code for the client. You also need to install a tomcat server on the pc, and then deploy the jsp file in the demo to tomcat. The specific method is Baidu.


Package com. lee. nethttp; import java. io. byteArrayOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. printWriter; import java. io. unsupportedEncodingException; import java.net. httpURLConnection; import java.net. URL; import android. app. activity; import android. OS. asyncTask; import android. OS. bundle; import android. util. base64; import android. util. log; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. editText; import android. widget. textView; public class MainActivity extends Activity {private EditText mEditText = null; private Button mButton = null; private TextView mTextView = null; private String mUrl = http: // 192.168.0.109: 8080/lee/index. jsp; // @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mEditText = (EditText) findViewById (R. id. edit); mButton = (Button) findViewById (R. id. button); mButton. setOnClickListener (mButtonClickListener); mTextView = (TextView) findViewById (R. id. text);} private OnClickListener mButtonClickListener = new OnClickListener () {@ Override public void onClick (View v) {String content = mEditText. getText (). toString (); content = toBase64 (content); String [] params = {content}; new nethttpasynctask(.exe cute (params) ;}; private class NetHttpAsyncTask extends AsyncTask
 
  
{@ Override protected String doInBackground (String... params) {String result = null; try {URL url = new URL (mUrl); HttpURLConnection connection = (HttpURLConnection) url. openConnection (); connection. setRequestMethod (POST); connection. setDoInput (true); connection. setDoOutput (true); connection. setUseCaches (false); connection. setRequestProperty (Content-type, application/x-www-form-urlencoded; charset = UTF-8); connection. setConnectTimeout (3000); connection. setReadTimeout (3000); PrintWriter writer = new PrintWriter (connection. getOutputStream (); String str = content + = + params [0]; writer. print (str); writer. flush (); writer. close (); InputStream input = connection. getInputStream (); ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream (); int ch; while (ch = input. read ())! =-1) {outputBuffer. write (ch);} result = outputBuffer. toString ();} catch (IOException e) {e. printStackTrace ();} return result;} @ Override protected void onPostExecute (String result) {mTextView. setText (result) ;}} private String toBase64 (String content) {try {content = Base64.encodeToString (content. getBytes (UTF-8), Base64.DEFAULT); Log. e (lee, toBase64 content = + content);} catch (UnsupportedEncodingException e) {e. printStackTrace () ;}return content ;}}
 



 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.