成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

IDEA后臺(tái)與安卓數(shù)據(jù)交互

Binguner / 2510人閱讀

摘要:實(shí)現(xiàn)功能發(fā)數(shù)據(jù)給后臺(tái),后臺(tái)根據(jù)獲取到的數(shù)據(jù)查詢數(shù)據(jù)庫(kù)并將對(duì)應(yīng)的數(shù)據(jù)發(fā)回客戶端顯示在界面開發(fā)工具,,端以下為需要新建或者修改的文件,以便新手學(xué)習(xí)客戶端運(yùn)行示例代碼展示登錄用戶名輸入框登錄密碼輸入框登錄按鈕

實(shí)現(xiàn)功能:Android app發(fā)ID數(shù)據(jù)給IDEA
后臺(tái),后臺(tái)根據(jù)獲取到的ID數(shù)據(jù)查詢數(shù)據(jù)庫(kù)并將對(duì)應(yīng)的數(shù)據(jù)發(fā)回客戶端顯示在app界面

開發(fā)工具:IDEA,Android studio,MySQL

Android端:(以下為需要新建或者修改的文件,以便新手學(xué)習(xí))
Java:MainActivity
GuestToServer
layout:activity_main
build.gradle(app)
AndoidManifest.xml
客戶端運(yùn)行示例:

代碼展示:

import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import org.json.JSONException;import org.json.JSONObject;import java.io.BufferedReader;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;public class MainActivity extends AppCompatActivity {    //登錄用戶名輸入框    private EditText et_username;    //登錄密碼輸入框    private EditText et_password;    //登錄按鈕    private EditText id;    private Button bt_login;    private TextView tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //獲取組件        init();        //對(duì)登錄按鈕的點(diǎn)擊監(jiān)控        bt_login.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                // Toast.makeText(LoginActivity.this, "登錄成功!", Toast.LENGTH_LONG).show();                final Handler myHandler = new Handler(){                    public void handleMessage(Message msg){                        String responseResult = (String)msg.obj;                        // Toast.makeText(LoginActivity.this, "登錄成功!", Toast.LENGTH_LONG).show();                        //登錄成功               System.out.println("response"+responseResult);                       try{                        JSONObject root = new JSONObject(responseResult);                           String userName = root.getString("userName");                           tv.append("userName"+"="+userName+"/n");                       }                       catch (JSONException e) {                           e.printStackTrace();                       }                        if(responseResult.equals("" +                                "true")){  Toast.makeText(com.example.myapplication.MainActivity.this, "登錄成功!", Toast.LENGTH_LONG).show();                        }                        //登錄失敗                        else{                 Toast.makeText(com.example.myapplication.MainActivity.this, "登錄失??!", Toast.LENGTH_LONG).show();                        }                    }                };                new Thread(new Runnable() {                    @Override                    public void run() {                        GuestToServer guestToServer = new GuestToServer();                        try {                            //如果是調(diào)用GuestToServer中驗(yàn)證用戶名與密碼的方法則使用下面句                           //String result = guestToServer.doPost(et_username.getText().toString().trim(), et_password.getText().toString().trim());                            String result = guestToServer.doPost(id.getText().toString().trim());                            Message msg = new Message();                         msg.obj = result;                            myHandler.sendMessage(msg);                        } catch (IOException e) {                            e.printStackTrace();                        }                    }                }).start();            }        });    }    /**     * 獲取組件     */    private void init() {        et_username = (EditText)findViewById(R.id.et_username);        et_password = (EditText)findViewById(R.id.et_password);        id= (EditText)findViewById(R.id.id);        bt_login = (Button)findViewById(R.id.bt_login);        tv = (TextView) findViewById(R.id.tv);//獲取到TextView組件    }}
 GuestToServer
import android.util.Log;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.HttpStatus;import org.apache.http.NameValuePair;import org.apache.http.client.HttpClient;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.util.ArrayList;import java.util.List;public class GuestToServer {    //localhost為本地主機(jī)IP地址/login為idea后臺(tái)項(xiàng)目名    private String url = "http://localhost:8080/login";    //服務(wù)器返回的結(jié)果//    String result = "";    /**     * 使用Post方式向服務(wù)器發(fā)送請(qǐng)求并返回響應(yīng)     *     * //如果使用驗(yàn)證用戶名及密碼的方法那么使用public String doPost(String username, String password) throws IOException {     * //且放出下面兩句參數(shù)設(shè)置    // * @param username 傳遞給服務(wù)器的username   //  * @param password 傳遞給服務(wù)器的password     *  @param id 傳遞給服務(wù)器的id     * @return     */    public String doPost(String id) throws IOException {        HttpClient httpClient = new DefaultHttpClient();        HttpPost httpPost = new HttpPost(url);        NameValuePair param3 = new BasicNameValuePair("id", id);        List<NameValuePair> params = new ArrayList<NameValuePair>();        params.add(param3);        //將參數(shù)包裝如HttpEntity中并放入HttpPost的請(qǐng)求體中        HttpEntity httpEntity = new UrlEncodedFormEntity(params, "GBK");        httpPost.setEntity(httpEntity);        HttpResponse httpResponse = httpClient.execute(httpPost);        //如果響應(yīng)成功        if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {            //得到信息體            HttpEntity entity = httpResponse.getEntity();            InputStream inputStream = entity.getContent();            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));            String readLine = null;            while ((readLine = br.readLine()) != null) {                result += readLine;            }            inputStream.close();            return result;        }        //響應(yīng)失敗        else {            return "false";        }    }    /*    public String doPost(String username, String password) throws IOException {        HttpClient httpClient = new DefaultHttpClient();        HttpPost httpPost = new HttpPost(url);        //將username與password參數(shù)裝入List中        NameValuePair param1 = new BasicNameValuePair("username", username);        NameValuePair param2 = new BasicNameValuePair("password", password);        List params = new ArrayList();        params.add(param1);        params.add(param2);        //將參數(shù)包裝如HttpEntity中并放入HttpPost的請(qǐng)求體中        HttpEntity httpEntity = new UrlEncodedFormEntity(params, "GBK");        httpPost.setEntity(httpEntity);        HttpResponse httpResponse = httpClient.execute(httpPost);        //如果響應(yīng)成功        if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {            //得到信息體            HttpEntity entity = httpResponse.getEntity();            InputStream inputStream = entity.getContent();            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));            String readLine = null;            while ((readLine = br.readLine()) != null) {                result += readLine;            }            inputStream.close();            return result;        }        //響應(yīng)失敗        else {            return "false";        }    }*/}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_login"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <TextView        android:text="登錄界面"        android:textSize="10dp"        android:textColor="#003399"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/textView1"        android:gravity="center_horizontal"        android:layout_margin="10dp"/>    <TextView        android:text="用戶名"        android:textSize="20dp"        android:textColor="#CC0000"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/textView2"        android:layout_margin="10dp"/>    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:inputType="textPersonName"        android:ems="10"        android:id="@+id/et_username"        android:textSize="10dp"        android:textColor="#003399"        android:layout_margin="10dp"/>    <TextView        android:text="密碼 "        android:textSize="20dp"        android:textColor="#CC0000"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/textView3"        android:layout_margin="10dp"/>    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:inputType="textPersonName"        android:ems="10"        android:id="@+id/et_password"        android:textSize="10dp"        android:textColor="#003399"        android:layout_margin="10dp"/>    <TextView        android:text="ID "        android:textSize="20dp"        android:textColor="#CC0000"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/idtextview"        android:layout_margin="10dp"/>    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:inputType="textPersonName"        android:ems="10"        android:id="@+id/id"        android:textSize="10dp"        android:textColor="#003399"        android:layout_margin="10dp"/>    <Button        android:text="登錄"        android:textSize="20dp"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/bt_login"        android:layout_marginLeft="20dp"        android:layout_marginRight="20dp"        android:background="#0099FF"/>    <TextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/tv"/></LinearLayout>

如果項(xiàng)目出現(xiàn)報(bào)錯(cuò),記得檢查報(bào)錯(cuò)日志,一般是apache沒有導(dǎo)入
在build.gradle里加入這行
useLibrary’org.apache.http.legacy’

由于版本不同,有些需要在manifest文件中的appication加入

<application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme"        android:usesCleartextTraffic="true">        <uses-library android:name="org.apache.http.legacy" android:required="false"/>

至此,Android app搭建完畢,可試運(yùn)行
IDEA后臺(tái)項(xiàng)目搭建:
可參考上一篇小程序后臺(tái),共用一個(gè)后臺(tái)與數(shù)據(jù)庫(kù)無(wú)需更改

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/122987.html

相關(guān)文章

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<