android http 本地 web服務(tomcat)

1. Android代碼

01
<b>public class JobActivity extends Activity {
02
    private TextView show;
03
    private EditText txt;
04
    private Button btn;
05
    
06
    /** Called when the activity is first created. */
07
    @Override
08
    public void onCreate(Bundle savedInstanceState) {
09
        super.onCreate(savedInstanceState);
10
        setContentView(R.layout.main);
11
        
12
        ConnectivityManager con=(ConnectivityManager)getSystemService(Activity.CONNECTIVITY_SERVICE);
13
        boolean wifi=con.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
14
        boolean internet=con.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting();
15
        if(wifi ){
16
            show = (TextView)findViewById(R.id.show);
17
            txt = (EditText)findViewById(R.id.txt);
18
            btn = (Button)findViewById(R.id.btn);
19
            btn.setOnClickListener(new OnClickListener() {
20
            @Override
21
               public void onClick(View v) {
22
                show.setText("dianjile");
23
                dopost(txt.getText().toString());
24
               }
25
              });
26
        }
27
    }
28
    
29
    private void dopost(String val){
30
        //封裝數據
31
        Map<String, String> parmas = new HashMap<String, String>();
32
        parmas.put("name", val);
33
       </b><p><b>        DefaultHttpClient client = new DefaultHttpClient();//http客戶端</b></p>
34
<p><b>        //這個地方需要註意:如果是調試器 </b><span style="color:#ff0000;font-family:verdana, 'ms song', 宋體, Arial, 微軟雅黑, Helvetica, sans-serif;text-align:left;white-space:normal;background-color:#ffffff;">10.0.2.2 本機地址;真機上:則是路由器中電腦地址</span></p>
35
<b>        HttpPost httpPost = new HttpPost("http://192.168.2.100:80/loowj/android$Android$go");
36
        ArrayList<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();
37
        if(parmas != null){
38
            Set<String> keys = parmas.keySet();
39
            for(Iterator<String> i = keys.iterator(); i.hasNext();) {
40
                 String key = (String)i.next();
41
                 pairs.add(new BasicNameValuePair(key, parmas.get(key)));
42
            }
43
       }
44
       
45
     try {
46
            UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(pairs, "utf-8");
47
            /*
48
             *  將POST數據放入HTTP請求
49
             */
50
            httpPost.setEntity(p_entity);
51
            /*
52
             *  發出實際的HTTP POST請求
53
              */
54
            TextView tv = (TextView)findViewById(R.id.state);
55
            tv.setText("start post:");
56
            HttpResponse response = client.execute(httpPost);
57
            tv.setText((response.getStatusLine().getStatusCode() +""));
58
                
59
            HttpEntity entity = response.getEntity();
60
            InputStream content = entity.getContent();
61
            String returnConnection = convertStreamToString(content);
62
            show.setText(returnConnection);
63
     } catch (IllegalStateException e) {
64
      e.printStackTrace();
65
     } catch (IOException e) {
66
      e.printStackTrace();
67
     }
68
    }
69
 
70
     private String convertStreamToString(InputStream is) {
71
      BufferedReader reader = new BufferedReader(new InputStreamReader(is));
72
            StringBuilder sb = new StringBuilder();
73
            String line = null;
74
            try {
75
                 while ((line = reader.readLine()) != null) {
76
                      sb.append(line);
77
                 }
78
            } catch (IOException e) {
79
                 e.printStackTrace();
80
            } finally {
81
                 try {
82
                      is.close();
83
                 } catch (IOException e) {
84
                      e.printStackTrace();
85
                 }
86
            }
87
            return sb.toString();
88
    }
89
}</b>

2.android 配置文件

1
<b><?xml version="1.0" encoding="utf-8"?>
2
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3
    package="com.loowj.core"
4
    android:versionCode="1"
5
    android:versionName="1.0" >
6
 
7
    <uses-sdk android:minSdkVersion="10" /></b>

01
<b>     <span style="color:#e53333;">//註意加上網絡權限</span>     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
02
    <uses-permission android:name="android.permission.INTERNET"/>
03
    <application
04
        android:icon="@drawable/ic_launcher"
05
        android:label="@string/app_name" >
06
        <activity
07
            android:name="loowj.JobActivity"
08
            android:label="@string/app_name" >
09
            <intent-filter>
10
                <action android:name="android.intent.action.MAIN" />
11
 
12
                <category android:name="android.intent.category.LAUNCHER" />
13
            </intent-filter>
14
        </activity>
15
    </application>
16
 
17
</manifest></b>
 

摘自 loowg

發佈留言