新手求助:Android应用对接Azure App Service实现文本交互(非.NET)
解决方案:无需.NET实现Android与Azure App Service的文本收发
Hey there! I get that the TodoItem-focused tutorials can feel overwhelming when all you need is simple text send/receive—let’s build this from scratch with no .NET required. Here’s a straightforward, beginner-friendly approach:
1. 先搞定Azure端:创建无.NET的App Service端点
We’ll use a Node.js Express server (super simple, no .NET involved) as your Azure App Service backend. Here’s what to do:
- 在Azure Portal创建一个新的App Service,选择Node.js作为运行时栈(不要选.NET相关选项),选免费/试用定价层就行。
- 编写一个极简的Express服务器,处理POST请求并返回文本反馈:
const express = require('express'); const app = express(); app.use(express.json()); // 解析JSON请求体 // 处理文本接收的端点 app.post('/api/receive-text', (req, res) => { const userInput = req.body.text; // 这里可以加简单的逻辑,比如生成反馈 const responseText = `Got your message: "${userInput}" - Thanks for sending!`; res.send(responseText); }); // 启动服务器(Azure会自动处理端口) const port = process.env.PORT || 3000; app.listen(port, () => { console.log(`Server running on port ${port}`); }); - 把这个代码部署到Azure App Service:你可以用Azure CLI、GitHub Actions,或者直接在Azure Portal的“高级工具”里用Kudu上传文件。部署后,测试一下端点(比如用Postman发POST请求到
https://your-app-name.azurewebsites.net/api/receive-text,请求体是{"text": "Hello Azure"},应该能收到反馈)。
2. Android端:发送文本并显示反馈
We’ll use OkHttp (a popular, easy-to-use HTTP library) to send the request. Let’s break this into parts:
第一步:添加依赖和权限
- 在你的
build.gradle (Module level)里添加OkHttp依赖:dependencies { implementation 'com.squareup.okhttp3:okhttp:4.12.0' } - 在
AndroidManifest.xml里添加互联网权限(必须!):<uses-permission android:name="android.permission.INTERNET" />
第二步:布局文件(activity_main.xml)
确保你有输入框、发送按钮和显示反馈的TextView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <EditText android:id="@+id/inputEditText" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter your text here"/> <Button android:id="@+id/sendButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send to Azure"/> <TextView android:id="@+id/feedbackTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="Feedback will appear here"/> </LinearLayout>
第三步:Activity逻辑(MainActivity.kt 示例)
异步发送请求并更新UI,避免阻塞主线程:
import android.os.Bundle import android.widget.Button import android.widget.EditText import android.widget.TextView import androidx.appcompat.app.AppCompatActivity import okhttp3.MediaType.Companion.toMediaType import okhttp3.OkHttpClient import okhttp3.Request import okhttp3.RequestBody.Companion.toRequestBody import kotlin.concurrent.thread class MainActivity : AppCompatActivity() { private val client = OkHttpClient() private val azureEndpoint = "https://your-app-name.azurewebsites.net/api/receive-text" // 替换成你的App Service地址 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val inputEditText = findViewById<EditText>(R.id.inputEditText) val sendButton = findViewById<Button>(R.id.sendButton) val feedbackTextView = findViewById<TextView>(R.id.feedbackTextView) sendButton.setOnClickListener { val userText = inputEditText.text.toString().trim() if (userText.isNotEmpty()) { // 异步发送请求 thread { sendTextToAzure(userText) { feedback -> // 在主线程更新UI runOnUiThread { feedbackTextView.text = feedback } } } } } } private fun sendTextToAzure(text: String, onResult: (String) -> Unit) { val json = """{"text": "$text"}""" val requestBody = json.toRequestBody("application/json; charset=utf-8".toMediaType()) val request = Request.Builder() .url(azureEndpoint) .post(requestBody) .build() client.newCall(request).execute().use { response -> if (response.isSuccessful) { val feedback = response.body?.string() ?: "No feedback received" onResult(feedback) } else { onResult("Error: ${response.code}") } } } }
3. 测试和注意事项
- 确保你的Azure App Service已经启动并正常运行,测试端点能正常返回反馈。
- Android端测试时,用真机或能访问互联网的模拟器。
- 如果遇到网络错误,检查App Service的防火墙设置(默认允许所有请求),以及Android端的权限是否正确添加。
这样就完成了!没有复杂的TodoItem表操作,就是简单的文本收发,完全不用.NET技术。
内容的提问来源于stack exchange,提问作者Mohamad Dia Abdulkarim
相关产品推荐
相关产品推荐

