วันพฤหัสบดีที่ 5 กรกฎาคม พ.ศ. 2555

Android http

--> Android HTTP URL Connection

HttpConnActivity.java Code

package android.example.http;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class HttpConnActivity extends Activity {
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        final TextView tv = (TextView)findViewById(R.id.tv);
       
        Button bn = (Button)findViewById(R.id.bn);
       
        bn.setOnClickListener(new OnClickListener() {
                  public void onClick(View v) {
                        HttpURLConnection conn = null;
                       
                      try {
                        conn = (HttpURLConnection)new URL("http://www.google.com").openConnection();
                                   
                        conn.setRequestMethod("GET");
                          conn.setInstanceFollowRedirects(true);
                          conn.setUseCaches(false);
                           
                        int resCode = conn.getResponseCode();
                        if (resCode == HttpURLConnection.HTTP_OK) {            
                              InputStream is = conn.getInputStream();
                              ByteArrayOutputStream bos = new ByteArrayOutputStream();
                              int read = 0;
                              while ((read = is.read()) != -1) {
                                    bos.write(read);
                              }
                              byte[] result = bos.toByteArray();
                              bos.close();
                                   
                              String data = new String(result);
                              tv.setText(data.substring(0, 500));
                        }
                        else {
                              tv.setText("resCode: " + resCode);
                        }
                                               
                        conn.disconnect();
                      }
                      catch (Exception e) {
                      }              
                       
                  }
            });
    }
}



main.xml Code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#f8b54b"
    android:orientation="vertical" >
   
    <Button
        android:id="@+id/bn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:text="Get http" />


    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="10dp"
        android:background="#4bf853"
        android:text="Data from http" />

</LinearLayout>

AndroidManifest.xml  Code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="android.example.http"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="7" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".HttpConnActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-permission android:name="android.permission.INTERNET"/>
       
</manifest>



Download Android HTTP Example Code -->




Android Control ควบคุมอุปกรณ์ต่างๆ ด้วย Android

สอนเขียน Android  สอนเขียนโปรแกรม Android