Various calling codes

Source: Internet
Author: User

Asynchronous task

Public class Myasynctask extends Asynctask<string, Integer, string> {
//define variable
HttpURLConnection Connection
Protected string Doinbackground (String ... params) {
StringBuffer buffer=null;
try {
URL url = new URL (params[0]);
Open connection
Connection = (httpurlconnection) url.openconnection ();
Timeout
Connection.setconnecttimeout (10*1000);
Determine if the return value is correct
if (Connection.getresponsecode () ==200) {
InputStream is= connection.getinputstream ();
BufferedReader reader = new BufferedReader (new InputStreamReader (IS));
Buffer = new StringBuffer ();
String str;
while ((Str=reader.readline ())!=null) {
Buffer.append (str);
}
Is.close ();
Reader.close ();
Connection.disconnect ();
System.out.println ("-----------Requested data:::" +buffer.tostring ());
}
} catch (Malformedurlexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
return buffer.tostring ();
}
}

 

Network Request data HTTP

public static string Getjsonstr (String urlstr) {
HttpClient client = new Defaulthttpclient ();
HttpGet get = new HttpGet (URLSTR);
try {
HttpResponse response = Client.execute (get);
if (Response.getstatusline (). Getstatuscode () = = 200) {
Return entityutils.tostring (Response.getentity (), "utf-8");
}
} catch (IOException e) {
E.printstacktrace ();
}
return null;
}

Imageloader Picture Loading

Public class Imageutils {

public void ImageUrl (final ImageView imageview,string path) {
Displayimageoptions options = new Displayimageoptions.builder ()
. Showimageforemptyuri (R.drawable.ic_launcher)
. Showimageonfail (R.drawable.ic_launcher)
. showimageonloading (R.drawable.ic_launcher)
. CacheInMemory ( true)
. Cacheondisk (True)
. Build ();
ImageSize ImageSize = new ImageSize (100, 100);
Imageloader.getinstance (). LoadImage (Path, imageSize, Options, new Simpleimageloadinglistener () {
@Override
public void Onloadingcomplete (String imageuri, view view, Bitmap loadedimage) {
Super.onloadingcomplete ( Imageuri, view, loadedimage);
Imageview.setimagebitmap (loadedimage);
}
});
}
}
public class Imageapplication extends application{

@Override
public void OnCreate () {
Imageloaderconfiguration configuration = new Imageloaderconfiguration.builder (this). Build ();
Imageloader.getinstance (). init (configuration);
}
}

Share

Share
Private Umsocialservice Mcontroller;
Mcontroller = Umservicefactory.getumsocialservice ("Com.umeng.login");
Set up Sina SSO handler
Mcontroller.getconfig (). Setssohandler (New Sinassohandler ());
Parameter 1 is the current activity, parameter 2 for the developer in the QQ Internet application app ID, Parameter 3 for the developer in the QQ Internet application app KEY.
Umqqssohandler Qqssohandler = new Umqqssohandler (This, "100424468",
"C7394704798a158208a74ab60104f0ba");
QQSSOHANDLER.ADDTOSOCIALSDK ();
Parameter 1 is the current activity, parameter 2 for the developer in the QQ Internet application app ID, Parameter 3 for the developer in the QQ Internet application app KEY.
Qzonessohandler Qzonessohandler = new Qzonessohandler (This, "100424468",
"C7394704798a158208a74ab60104f0ba");
QZONESSOHANDLER.ADDTOSOCIALSDK ();
Mcontroller.openshare (Infoactivity.this, false);

Intent Intent = Getintent ();
String des = Intent.getstringextra ("des");
LOG.I ("url", des);
WebView Web = (WebView) Findviewbyid (R.id.webview);
Web.loadurl (DES);
Web.setwebviewclient (New Webviewclient () {
@Override
public boolean shouldoverrideurlloading (WebView view, String URL) {
TODO auto-generated Method Stub
Return super.shouldoverrideurlloading (view, URL);
}
});
Button share = (Button) Findviewbyid (R.id.share);
Button QQ = (button) Findviewbyid (R.ID.QQ);
Button Xinlang = (button) Findviewbyid (R.id.xinlang);
Button Qqkong = (button) Findviewbyid (R.id.qqkong);
Share.setonclicklistener (this);
Qq.setonclicklistener (this);
Xinlang.setonclicklistener (this);
Qqkong.setonclicklistener (this);
Switch (V.getid ()) {
Case R.id.share:
LinearLayout ll = (linearlayout) Findviewbyid (R.ID.LL);
Ll.setvisibility (view.visible);
Break
Case R.ID.QQ:
Sharedata (Share_media. QQ);
Break
Case R.id.xinlang:
Sharedata (Share_media. SINA);
Break
Case R.id.qqkong:
Sharedata (Share_media. QZONE);
Break
Default
Break
}
private void Sharedata (Share_media platform) {

Mcontroller.dooauthverify (Infoactivity.this, Platform, New Umauthlistener () {
@Override
public void OnStart (Share_media platform) {
Toast.maketext (Infoactivity.this, "Licensing start", Toast.length_short). Show ();
}
@Override
public void OnError (Socializeexception e, Share_media platform) {
Toast.maketext (infoactivity.this, "Authorization Error", Toast.length_short). Show ();
}
@Override
public void OnComplete (Bundle value, Share_media platform) {
Toast.maketext (Infoactivity.this, "authorized completion", Toast.length_short). Show ();
Get relevant licensing information
Mcontroller.getplatforminfo (Infoactivity.this, Platform, New Umdatalistener () {
@Override
public void OnStart () {
Toast.maketext (Infoactivity.this, "Get Platform data start ...", Toast.length_short). Show ();
}
@Override
public void OnComplete (int status, map<string, object> info) {
if (status = = && info! = null) {
StringBuilder sb = new StringBuilder ();
set<string> keys = Info.keyset ();
for (String Key:keys) {
Sb.append (key+ "=" +info.get (key). ToString () + "\ r \ n");
}
LOG.D ("TestData", sb.tostring ());
}else{
LOG.D ("TestData", "Error occurred:" +status);
}
}
});
}
@Override
public void OnCancel (Share_media platform) {
Toast.maketext (Infoactivity.this, "authorization cancellation", Toast.length_short). Show ();
}
} );
Setting up shared content
Mcontroller.setsharecontent ("Friends League social Component (SDK) enables mobile apps to quickly integrate social sharing features, http://www.umeng.com/social");
Set up Share pictures
Mcontroller.setsharemedia (New Umimage (Infoactivity.this, "http://www.umeng.com/images/pic/banner_module_social.png"));
Share directly
Mcontroller.directshare (Infoactivity.this, platform,
New Snspostlistener () {
@Override
public void OnStart () {
Toast.maketext (Infoactivity.this, "Share Start", Toast.length_short). Show ();
}
@Override
public void OnComplete (Share_media platform,int ecode, socializeentity entity) {
if (Ecode = = statuscode.st_code_successed) {
Toast.maketext (Infoactivity.this, "sharing success", Toast.length_short). Show ();
}else{
Toast.maketext (Infoactivity.this, "Sharing failed", Toast.length_short). Show ();
}
}

});
}

XML parsing

public class Parsexml {

private static tweet tweet;

public static list<tweet> GetData (InputStream is) {

list<tweet> list = new arraylist<tweet> ();
Xmlpullparser parser = Xml.newpullparser ();
try {
Parser.setinput (IS, "utf-8");
int eventtype = Parser.geteventtype ();
String tagName = null;
while (eventtype! = xmlpullparser.end_document) {
Switch (eventtype) {
Start parsing documents
Case Xmlpullparser.start_document:
Break
Parse to start tag
Case Xmlpullparser.start_tag:
Get the element label signature that is being parsed
TagName = Parser.getname ();
Determines whether the element tag name being parsed is a person
if (tagname.equals ("tweet")) {//label signature is person
Create object for Person
Tweet = new Tweet ();
}
Break
Parse to Text
Case Xmlpullparser.text:
Get the text content that is currently being parsed
String text = Parser.gettext ();
Determines which element node the text content is being parsed in.
if (Tagname.equals ("id")) {
Tweet.setid (Integer.parseint (text));
} else if (Tagname.equals ("Portrait")) {
Tweet.setportrait (text);
} else if (Tagname.equals ("author")) {
Tweet.setauthor (text);
} else if (Tagname.equals ("Body")) {
Tweet.setbody (text);
} else if (Tagname.equals ("PubDate")) {
Tweet.setpubdate (text);
}else if (tagname.equals ("Imgsmall")) {
Tweet.setimgsmall (text);
}
Break
Resolve to end tag
Case Xmlpullparser.end_tag:
Get the element label signature that is being parsed
TagName = Parser.getname ();
Determines whether the element tag name being parsed is a person
if (tagname.equals ("tweet")) {//If the end tag resolves to person, the explanation is finished
To load a person object into a collection
List.add (tweet);
Tweet = null;
}
TagName = "";
Break
End of document parsing
Case Xmlpullparser.end_document:
Break
}
This event is processed and goes to the next node: Next event
EventType = Parser.next ();
}
} catch (Xmlpullparserexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
return list;
}
}

Set rounded picture

Displayimageoptions options = new Displayimageoptions.builder ()
. Displayer (New Roundedbitmapdisplayer (90))//Set rounded picture
. build ();

Progress bar shows the process of requesting a network

1. Declaring variables
Private Handler Handler = new Handler ();
Private ProgressDialog progressdialog = null;

2. Show progress Bar dialog box when starting network connection
ProgressDialog = Progressdialog.show (myactivity.this, "Please wait ...", "Get Data ...", true);

3. Update the interface in Handler.post
public void Setlistadapter () {
Handler.post (New Runnable () {
public void Run () {
Update the acquired data to the list
Mylistview.setadapter (Saimageitems);
}
}
});

4. Open a new thread for network connection to get data
New Thread (New Runnable () {
@Override
public void Run () {
TODO auto-generated Method Stub
Requesting data from the server
Maplist=myapi.getalldatas ();
Setlistadapter (maplist);
Close the dialog box when the list data is updated
Progressdialog.dismiss ();
}}). Start ();

Various calling codes

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.