Android 台灣中文網

標題: ANDROID如何連接伺服器 [打印本頁]

作者: pinkwow84522    時間: 2016-5-11 19:37
標題: ANDROID如何連接伺服器
package com.example.Family;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class enter extends Activity {
        private Button btn1,btn2;
        private EditText id,psw;
        Thread Login;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.enter);


        id=(EditText)findViewById(R.id.editText1);
        psw=(EditText)findViewById(R.id.editText2);

        btn1=(Button)findViewById(R.id.button1);
        btn2=(Button)findViewById(R.id.button2);

         btn1.setOnClickListener(new OnClickListener()
        {


                @Override         
           public void onClick(View arg0)
            {
                       
                        //Intent intent = new Intent();
            //intent.setClass(enter.this, home.class);
            //enter.this.startActivity(intent);
                        Login = new Login();
                        Login.start();
                       
                   
                  int a=0;
                           if("".equals(id.getText().toString()))
                                   a=1;
                           else if("".equals(psw.getText().toString()))
                                   a=1;   //如果是空密碼
                           if(a==1)
                           {
                                   Toast toast = Toast.makeText(getApplicationContext(),
                                                   "腦袋空空嗎~請輸入!", Toast.LENGTH_LONG);
                                   toast.show();
                           }
                  

                       
            }
        }
                );

        btn2.setOnClickListener(new OnClickListener()
        {
                @Override       
                        public void onClick(View arg0) {
                       
                                Intent intent = new Intent();
                    intent.setClass(enter.this, new1.class);
                    enter.this.startActivity(intent);
                        }
                });

    }

   class Login extends Thread{
               
                @Override
                public void run(){
                               
        //建立HttpClient用以跟伺服器溝通
                       
                HttpClient client = new DefaultHttpClient();
                       
                try {
               
                HttpPost post = new HttpPost("http://192.192.140.227/~D10216216/aaa.php");
                                
        //建立POST的變數
                List< NameValuePair> vars = new ArrayList<NameValuePair>();
                
                vars.add(new BasicNameValuePair("member_id",id.getText().toString()));
                vars.add(new BasicNameValuePair("password",psw.getText().toString()));
               
                         
                post.setEntity(new UrlEncodedFormEntity(vars,HTTP.UTF_8));
                         
                         
        //建立ResponseHandler,以接收伺服器回傳的訊息
                ResponseHandler<String> h=new BasicResponseHandler();
                               
        //將回傳的訊息轉為String
                String response=new String(client.execute(post,h).getBytes(),HTTP.UTF_8);
                String success = "Login Succeeded";
                               
                Looper.prepare();
                               
        //若回傳的訊息等於"Login Succeeded",跳轉到另一個頁面
                               
                if(response.equals(success)){
                               
                Intent i = new Intent(enter.this,home.class);
                startActivity(i);
                                       
                }
                               
        //否則只顯示回傳訊息
                               
                else{
                               
                Toast.makeText(enter.this, response, Toast.LENGTH_LONG).show();
                               
                }
                               
                Looper.loop();
                       
        }       
                       
                       
                catch(Exception ex){
                               
                //若伺服器無法與PHP檔連接時的動作
                                               

                               
                               
                }

                       
             }
               
        }               
                     
                                                   
                                   
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // 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);
    }
}



作者: ploglin    時間: 2016-5-12 23:32
是我眼睛有問題嗎?沒看懂要問什麼問題?
作者: proshenn    時間: 2016-5-17 00:33
Web API 的部分可以參考微軟的官方教學
作者: pinkwow84522    時間: 2016-5-18 09:49
ploglin 發表於 2016-5-12 23:32
是我眼睛有問題嗎?沒看懂要問什麼問題?

我想連到遠端伺服器可是都連不上去想說下面連的地方是有寫錯還是少寫什麼
作者: ploglin    時間: 2016-5-20 11:41
pinkwow84522 發表於 2016-5-18 09:49
我想連到遠端伺服器可是都連不上去想說下面連的地方是有寫錯還是少寫什麼 ...

看起來應該沒什麼問題,你檢查看看你的API服務有沒正常,192.192開頭是內部IP嗎?如果是的話手機也要連內網才可以。
再來就是檢查有沒有放存取 Internet 的權限




歡迎光臨 Android 台灣中文網 (https://apk.tw/) Powered by Discuz! X3.1