Android uses get, Post to get network data

Source: Internet
Author: User

The first is get:

Layout is very simple, just a button, the main look at Mainactivity bar:

Package Com.francis.httpget;import Android.app.activity;import Android.os.asynctask;import android.os.Bundle; Import Android.view.menu;import android.view.menuitem;import android.view.view;import java.io.BufferedReader; Import Java.io.ioexception;import Java.io.inputstream;import Java.io.inputstreamreader;import Java.net.malformedurlexception;import Java.net.url;import Java.net.urlconnection;public class MyActivity extends    Activity {BufferedReader BufferedReader;    InputStreamReader InputStreamReader;    InputStream InputStream;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_my); Findviewbyid (R.id.button). Setonclicklistener (New View.onclicklistener () {@Override public void Oncl Ick (View v) {new asynctask<string,void,void> () {@Override Pro        tected Void doinbackground (String ... params) {                try {URL url = new URL (params[0]);                            URLConnection connection = Url.openconnection ();                            InputStream = Connection.getinputstream ();                            InputStreamReader = new InputStreamReader (InputStream, "UTF-8");                            BufferedReader = new BufferedReader (InputStreamReader);                            String Line;                                    while (true) {if (line = Bufferedreader.readline ()) = null) {                                System.out.println (line); }}} catch (Exception e) {E.printstacktrace                        ();                                } finally {try {bufferedreader.close ();                                Inputstreamreader.close (); InputstrEam.close ();                            } catch (IOException e) {e.printstacktrace ();                    }} return null; }}.execute ("http://fanyi.youdao.com/openapi.do?keyfrom=httpgetfrancis&key=893755706&type=data&            doctype=xml&version=1.1&q= here is Youdao translation API ");    }        }); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Actio        n Bar if it is present.        Getmenuinflater (). Inflate (r.menu.my, menu);    return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here.  The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent        Activity in Androidmanifest.xml.        int id = item.getitemid ();        if (id = = r.id.action_settings) {    return true;    } return super.onoptionsitemselected (item); }}

URL for Youdao translation of the API provided, very convenient. Console output



Next is the post:

Quite simply, here are only the click events for the button that submitted the POST request:

There are 2 points to note when compared to get:

1.url is separate from the parameters;

2.connection.setdoinput (TRUE);
Connection.setdooutput (TRUE);
Connection.setrequestmethod ("POST");

Post Findviewbyid (R.id.button2). Setonclicklistener (New View.onclicklistener () {@Override                    public void OnClick (View v) {new asynctask<string,void,void> () {@Override  Protected Void doinbackground (String ... params) {try {URL                            url = new URL (params[0]);                            HttpURLConnection connection = (httpurlconnection) url.openconnection ();                            Connection.setdoinput (TRUE);                            Connection.setdooutput (TRUE);                            Connection.setrequestmethod ("POST");                            OutputStream = Connection.getoutputstream ();                            OutputStreamWriter = new OutputStreamWriter (OutputStream, "UTF-8");                            BufferedWriter = new BufferedWriter (outputstreamwriter); Bufferedwriter.write ("Keyfrom=httpgetfrancis&key=893755706&type=data&doctype=xml&version=1.1&q= here is Youdao translation API ");                            Bufferedwriter.flush ();                            InputStream = Connection.getinputstream ();                            InputStreamReader = new InputStreamReader (InputStream, "UTF-8");                            BufferedReader = new BufferedReader (InputStreamReader);                            String Line;                                    while (true) {if (line = Bufferedreader.readline ()) = null) {                                System.out.println (line); }}} catch (Exception e) {E.printstacktrace                        ();                                } finally {try {bufferedreader.close ();                                Inputstreamreader.close ();                 Inputstream.close ();           } catch (IOException e) {e.printstacktrace ();                    }} return null;            }}.execute ("Http://fanyi.youdao.com/openapi.do"); }        });


Android uses get, Post to get network data

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.