【Android-网络通讯】 客户端与.Net服务端Http通讯

以登陆系统为例:

一、创建服务端程序

1、打开VS2012,新建项目,创建ASP.NET WEB应用程序 ,命名为MyApp

2、添加新建项,选择一般处理程序,创建Login.ashx

 

C# Code:  Login.ashx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MyApp.Remote
{
    /// <summary>
    /// Login 的摘要说明
    /// </summary>
    public class Login : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            switch (context.Request["type"])
            {
                case "login":
                    loginValidate(context);
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 验证登陆
        /// </summary>
        /// <param name="context"></param>
        private void loginValidate(HttpContext context)
        {
            string account = context.Request["Account"].ToString();
            string password = context.Request["Password"].ToString();
            if (account == "123" && password == "123")
            {
                string realName="HelloWord";           
                context.Response.Write("{\"Result\":\"1\",\"RealName\":\""+realName+"\"}");
                //实际输出:{"Result":"1","RealName":"HelloWord"}
                //注意:双引号需要用转义符\
            }
            else
            {
                context.Response.Write("{\"Result\":\"0\"}");
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

3、到这里就完成服务端的验证代码登陆了。

浏览器输入地址访问:http://localhost:11946/Remote/Login.ashx?type=login&Account=123&Password=123  

4、这时候程序还没有部署到IIS,那么如何在VS调试的时候,客户端可以通过IP访问该程序?

 客户端通过IP和端口访问服务端程序

 

二、创建客户端程序

1、界面布局 layout \ activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="10dp"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="账号" />

        <EditText
            android:id="@+id/edittext_loginAccount"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="number" >

            <requestFocus />
        </EditText>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="密码" />

        <EditText
            android:id="@+id/editetext_loginPassword"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ems="10"
            android:inputType="textPassword" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <CheckBox
            android:id="@+id/checkbox_remindPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="记住密码" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <Button
            android:id="@+id/button_login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="登录" />
    </LinearLayout>

</LinearLayout>

2、Java Code : MainActivity.java

package com.example.net;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    private String ServerUrl = "http://192.168.137.210:11946/Remote/";
    private EditText et_loginAccount;
    private EditText et_loginPassword;
    private CheckBox cb_remindPassword;
    private Button btn_login;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());

        et_loginAccount = (EditText) findViewById(R.id.edittext_loginAccount);
        et_loginPassword = (EditText) findViewById(R.id.editetext_loginPassword);
        cb_remindPassword = (CheckBox) findViewById(R.id.checkbox_remindPassword);
        btn_login = (Button) findViewById(R.id.button_login);

        btn_login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // loading 对话框
                progressDialog = ProgressDialog.show(MainActivity.this, "", "服务器连接中...", true, false);
                // 开启线程去验证登录
                new Thread() {
                    @Override
                    public void run() {
                        // 向handler发消息
                        mHandler.sendEmptyMessage(0);
                    }
                }.start();
            }
        });

    }

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // 登录验证
            loginValidate();
        }

    };

    private void loginValidate() {
        // 打开网络连接
        HttpClient client = new DefaultHttpClient();
        StringBuilder builder = new StringBuilder();
        // 服务器提交地址
        String url = ServerUrl + "Login.ashx?type=login&Account=" + et_loginAccount.getText().toString() + "&Password=" + et_loginPassword.getText().toString();
        HttpGet httpGet = new HttpGet(url);
        try {
            HttpResponse response = client.execute(httpGet);
            BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            // 填充数据流
            for (String s = reader.readLine(); s != null; s = reader.readLine()) {
                builder.append(s);
            }
            // 读取Json返回数组
            JSONObject jsonObject = new JSONObject(builder.toString());
            String re_result = jsonObject.getString("Result");
            String re_realName = jsonObject.getString("RealName");
            if (re_result.equals("1")) {
                Toast.makeText(MainActivity.this, "验证成功!" + re_realName, Toast.LENGTH_SHORT).show();
                // TODO:跳转页面
            } else {
                Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
            }
            progressDialog.dismiss();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(MainActivity.this, "服务器数据读取失败", Toast.LENGTH_SHORT).show();
            progressDialog.dismiss();
        }
    }

}

 

StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());

 少了上面两句代码会报错android.os.NetworkOnMainThreadException即,在主线程访问网络时出的异常

Android在4.0之前的版本支持在主线程中访问网络,但是在4.0以后对这部分程序进行了优化,也就是说访问网络的代码不能写在主线程中了。

稍后研究多线程

3、别放了加上权限 AndroidManifest.xml

  <!-- sd卡读取权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

    <!-- 访问网络权限 -->
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <!-- 完全退出程序权限 -->
    <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />

 

转载于:https://www.cnblogs.com/Sukie-s-home/p/8385700.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Integration WebFlux提供了一种基于反应式编程模型的流式交互方式,它可以让客户端服务端之间实现数据的实时推送和响应。 以下是使用Spring Integration WebFlux实现客户端服务端流式交互的步骤: 1. 配置服务端 首先,需要配置一个WebFlux服务器,以便客户端可以连接到它并发送请求。可以使用Spring Boot来创建WebFlux服务器,如下所示: ```java @SpringBootApplication public class WebFluxServer { public static void main(String[] args) { SpringApplication.run(WebFluxServer.class, args); } @Bean public RouterFunction<ServerResponse> route(ServerHandler handler) { return RouterFunctions.route(RequestPredicates.GET("/stream"), handler::stream); } } ``` 在此示例中,创建了一个WebFlux服务器,并注册了一个名为“route”的路由函数,将HTTP GET请求映射到名为“stream”的处理程序方法上。 2. 创建服务端处理程序 接下来,需要创建一个处理程序,该处理程序将接收来自客户端的请求,并将其转换为流式响应。可以使用Spring Integration WebFlux提供的FluxSink来实现这一点,如下所示: ```java @Component public class ServerHandler { public Mono<ServerResponse> stream(ServerRequest request) { Flux<String> flux = Flux.interval(Duration.ofSeconds(1)) .map(l -> "Tick #" + l) .take(10); return ServerResponse.ok() .contentType(MediaType.TEXT_EVENT_STREAM) .body(BodyInserters.fromProducer(flux, String.class)); } } ``` 在此示例中,创建了一个名为“stream”的处理程序方法,该方法将返回一个Flux<String>实例,该实例每秒发出一次字符串“Tick #x”,其中x是自增的数字。然后,使用ServerResponse.ok()创建一个响应对象,并将其内容类型设置为text/event-stream。最后,使用BodyInserters.fromProducer()方法将Flux<String>实例转换为响应体。 3. 配置客户端服务端类似,客户端也需要配置一个WebFlux客户端,以便可以连接到服务端并发送请求。可以使用Spring Boot来创建WebFlux客户端,如下所示: ```java @SpringBootApplication public class WebFluxClient { public static void main(String[] args) { SpringApplication.run(WebFluxClient.class, args); } @Bean public CommandLineRunner run(WebClient client) { return args -> { client.get() .uri("http://localhost:8080/stream") .accept(MediaType.TEXT_EVENT_STREAM) .exchange() .flatMapMany(response -> response.bodyToFlux(String.class)) .subscribe(System.out::println); }; } } ``` 在此示例中,创建了一个WebFlux客户端,并使用CommandLineRunner接口将其运行在Spring Boot应用程序中。在run()方法中,创建了一个WebClient实例,并使用它来发送HTTP GET请求到服务端的“/stream”端点。然后,将响应体转换为Flux<String>实例,并使用subscribe()方法打印每个字符串。 4. 运行客户端服务端 现在,可以同时运行客户端服务端应用程序,并观察到服务端每秒向客户端发送一条消息,直到发送了10条消息。 通过上述步骤,就可以使用Spring Integration WebFlux实现客户端服务端之间的流式交互。这种方式可以用于实现实时通信、事件驱动的处理等应用场景。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值