Android StudioHttp协议GET连接

本文介绍了如何在Android Studio中使用Http协议进行GET连接,详细阐述了相关代码实现过程。
摘要由CSDN通过智能技术生成

代码内容

package com.example.day13;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class 
要在 Android Studio连接 Smart Tomcat,可以按照以下步骤操作: 1. 在 Smart Tomcat 中启动 Web 应用程序,并确保应用程序在运行状态下。 2. 在 Android Studio 中,打开项目并打开 "build.gradle(Module: app)" 文件。 3. 在文件中,添加以下代码: ``` android { ... defaultConfig { ... // 添加以下代码 manifestPlaceholders = [ 'appPackageName': 'com.example.myapp', // 替换成应用程序包名 'tomcatHost': '192.168.0.100', // 替换成 Smart Tomcat 的 IP 地址 'tomcatPort': '8080' // 替换成 Smart Tomcat 的端口号 ] } ... } ``` 4. 在 AndroidManifest.xml 文件中,添加以下权限: ``` <uses-permission android:name="android.permission.INTERNET" /> ``` 5. 在 AndroidManifest.xml 文件中,添加以下代码: ``` <application ... <uses-library android:name="org.apache.http.legacy" android:required="false" /> ... </application> ``` 6. 在 Android Studio 中,创建一个新的 Java 类,并添加以下代码: ``` import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class TomcatConnection { private static final int CONNECTION_TIMEOUT = 5000; private static final int READ_TIMEOUT = 5000; public static String sendHttpRequest(String urlString) { HttpURLConnection connection = null; InputStream inputStream = null; InputStreamReader inputStreamReader = null; BufferedReader bufferedReader = null; StringBuffer stringBuffer = new StringBuffer(); try { URL url = new URL(urlString); connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(CONNECTION_TIMEOUT); connection.setReadTimeout(READ_TIMEOUT); connection.setRequestMethod("GET"); connection.connect(); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { inputStream = connection.getInputStream(); inputStreamReader = new InputStreamReader(inputStream); bufferedReader = new BufferedReader(inputStreamReader); String line; while ((line = bufferedReader.readLine()) != null) { stringBuffer.append(line); } } } catch (IOException e) { e.printStackTrace(); } finally { try { if (bufferedReader != null) { bufferedReader.close(); } if (inputStreamReader != null) { inputStreamReader.close(); } if (inputStream != null) { inputStream.close(); } if (connection != null) { connection.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } return stringBuffer.toString(); } } ``` 7. 在需要连接 Smart Tomcat 的地方,使用以下代码: ``` String url = "http://" + BuildConfig.tomcatHost + ":" + BuildConfig.tomcatPort + "/myapp/somepath"; String response = TomcatConnection.sendHttpRequest(url); ``` 这样就可以在 Android Studio连接 Smart Tomcat 了。需要注意的是,这里使用的是 HTTP 协议,如果需要使用 HTTPS 协议,则需要对代码进行修改。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值