綁定帳號登入

Android 台灣中文網

打印 上一主題 下一主題

[求助] ANDROID如何連接伺服器

[複製連結] 查看: 1584|回覆: 4|好評: 0
跳轉到指定樓層
樓主
pinkwow84522 | 收聽TA | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
發表於 2016-5-11 19:37

馬上加入Android 台灣中文網,立即免費下載應用遊戲。

您需要 登錄 才可以下載或查看,沒有帳號?註冊

x
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);
    }
}


「用Android 就來APK.TW」,快來加入粉絲吧!
Android 台灣中文網(APK.TW)

評分

參與人數 1碎鑽 +3 收起 理由
Leung-Jacky + 3 很給力!

查看全部評分

收藏收藏 分享分享 分享專題
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

沙發
ploglin | 收聽TA | 只看該作者
發表於 2016-5-12 23:32
是我眼睛有問題嗎?沒看懂要問什麼問題?
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

板凳
proshenn | 收聽TA | 只看該作者
發表於 2016-5-17 00:33
Web API 的部分可以參考微軟的官方教學
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

地板
 樓主| pinkwow84522 | 收聽TA | 只看該作者
發表於 2016-5-18 09:49
由 手機網頁 發佈
ploglin 發表於 2016-5-12 23:32
是我眼睛有問題嗎?沒看懂要問什麼問題?

我想連到遠端伺服器可是都連不上去想說下面連的地方是有寫錯還是少寫什麼
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

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

看起來應該沒什麼問題,你檢查看看你的API服務有沒正常,192.192開頭是內部IP嗎?如果是的話手機也要連內網才可以。
再來就是檢查有沒有放存取 Internet 的權限
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則