Android article-How to do a simple Android source code Viewer

Source: Internet
Author: User

1, Web Source viewer:

HttpURLConnection: Used to send or receive data

Mainactivity article:

Import Java.io.InputStream;

Import java.net.HttpURLConnection;

Import java.net.MalformedURLException;

Import Java.net.URL;


Import Android.os.Bundle;

Import Android.os.Handler;

Import Android.os.Looper;

Import Android.os.Message;

Import android.app.Activity;

Import Android.view.Menu;

Import Android.view.View;

Import Android.widget.EditText;

Import Android.widget.TextView;

Import Android.widget.Toast;


public class Mainactivity extends Activity {


protected final int requestsucess = 0; CTRL + SHIFT + X Y

protected final int requestnotfound = 1;

protected final int requestexception = 2;

Private EditText Et_path;

Private TextView tv_reuslt;


Define a handler in the main thread

Private Handler Handler = new Handler () {

This method is executed in the main thread.

public void Handlemessage (Android.os.Message msg) {

So you can update the UI in the main thread.

[1] distinguish which message is sent

Switch (msg.what) {

Case Requestsucess://Representative Request succeeded

String content = (string) msg.obj;

Tv_reuslt.settext (content);

Break

Case Requestnotfound://Representative Request succeeded

Toast.maketext (Getapplicationcontext (), "Request resource does not exist", 0). Show ();

Break

Case Requestexception://Representative Request succeeded

Toast.maketext (Getapplicationcontext (), "Server Busy please later ....", 1). Show ();

Break

Default

Break

}

};

};


@Override

protected void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.activity_main);

[1] Find the controls we care about

Et_path = (EditText) Findviewbyid (R.id.et_path);

Tv_reuslt = (TextView) Findviewbyid (R.id.tv_result);

[1.1] Print the name of the current thread

System.out.println ("Current thread Name:" +thread.currentthread (). GetName ());

}


[2] Click the button to view the source code of the specified path

public void Click (View v) {

[2.0] Creating a child thread

New Thread () {public void run () {

try {

[2.1] Get the source path

String path = Et_path.gettext (). toString (). Trim ();

[2.2] Create URL object specify the URL (path) we want to access

URL url = new URL (path);

[2.3] Get HttpURLConnection object for sending or receiving data

HttpURLConnection conn = (httpurlconnection) url.openconnection ();

[2.4] Setting send GET request

Conn.setrequestmethod ("get");//get requires that the default is a GET request

[2.5] Setting the request time-out period

Conn.setconnecttimeout (5000);

[2.6] Get the status code returned by the server

int code = Conn.getresponsecode ();

[2.7] if code = = 200 indicates a successful request

if (code = = 200) {

[2.8] Gets the data returned by the server is returned in the form of a stream because it is a very common operation to change the flow into a string so I pulled out a tool class (Utils)

InputStream in = Conn.getinputstream ();

[2.9] Using our defined tool class to convert in to string

String content = Streamtools.readstream (in);

2.9.0 Creating a Message Object

Message msg = new Message ();

Msg.what = requestsucess;

Msg.obj = content;

2.9.1 took the handler we created and told the system that I wanted to update the UI.

Handler.sendmessage (msg); sent a message (msg) to put the data in the MSG Handlemessage method will be executed

[2.9.1] shows the data in the stream on the TextView, which is the logic of updating the UI.

tv_reuslt.settext (content);

}else{

Request resource does not exist toast is a view and cannot update UI in thread

Message msg = new Message ();

Msg.what = requestnotfound;//represents which message

Handler.sendmessage (msg);

}

} catch (Exception e) {

E.printstacktrace ();

Message msg = new Message ();

Msg.what = requestexception;//represents which message

Handler.sendmessage (msg); Send Message

}

};}. Start ();

}

}





















Streamtools and mainactivity in the same bag.

Import Java.io.ByteArrayOutputStream;

Import Java.io.InputStream;


public class Streamtools {


Convert a inputstream into a string

public static String Readstream (InputStream in) throws exception{

Define a memory output stream

Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();

int len =-1;

byte[] buffer = new byte[1024]; 1kb

while ((Len=in.read (buffer))!=-1) {

Baos.write (buffer, 0, Len);

}

In.close ();

String content = new String (Baos.tobytearray ());

return content;

}

}






Activity_main article:

<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:orientation= "Vertical"

Tools:context= ". Mainactivity ">


<edittext

Android:id= "@+id/et_path"

Android:layout_width= "Match_parent"

android:layout_height= "Wrap_content"

Android:hint= "Please enter view url"/>


<button

Android:layout_width= "Wrap_content"

android:layout_height= "Wrap_content"

android:onclick= "click"

android:text= "View"/>


<scrollview

Android:layout_width= "Match_parent"

android:layout_height= "Wrap_content" >


<textview

Android:id= "@+id/tv_result"

Android:layout_width= "Match_parent"

android:layout_height= "Match_parent"

android:text= "Hagha"/>

</ScrollView>


</LinearLayout>



Finally, the most important thing is to not forget to configure the license

Android article-How to do a simple Android source code Viewer

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.