java代码发送http接口获取json数据的案例

public class GetWheather {

	 
	
	public static String  getWeatherInfo(){
		   RequestConfig requestConfig = RequestConfig.custom()  
	             .setConnectTimeout(5000).setConnectionRequestTimeout(1000)  
	             .setSocketTimeout(5000).build(); 
		String httpUrl = "http://www.weather.com.cn/data/cityinfo/101110701.html";//http://www.weather.com.cn/data/cityinfo/101010100.html

		HttpGet httpGet = new HttpGet(httpUrl);// 创建get请求

		CloseableHttpClient httpClient = null;  

		CloseableHttpResponse response = null;  

		HttpEntity entity = null;  

		String responseContent = null;  
		 String str= null;
		try {  

		     // 创建默认的httpClient实例.  

		     httpClient = HttpClients.createDefault();  

		     httpGet.setConfig(requestConfig);  

		     // 执行请求  

		     response = httpClient.execute(httpGet);  

		     entity = response.getEntity();  

		     responseContent = EntityUtils.toStrin
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Unity WebGL中使用Java,您需要编写一个Unity插件来实现两者之间的通信。以下是一个简单的插件示例,可以让Unity从Java获取数据。 首先,在Java中创建一个类,该类将包含您要在Unity中访问的数据: ```java public class JavaData { private int intValue; private String stringValue; public JavaData(int intValue, String stringValue) { this.intValue = intValue; this.stringValue = stringValue; } // Getters for the data public int getIntValue() { return intValue; } public String getStringValue() { return stringValue; } } ``` 接下来,在Java中创建一个接口,该接口将由Unity插件调用以获取数据: ```java public interface JavaInterface { public JavaData getData(); } ``` 在Unity中,创建一个C#脚本来实现该插件: ```csharp using System.Runtime.InteropServices; public class JavaPlugin : MonoBehaviour { // Import the JavaInterface [DllImport("__Internal")] private static extern void getJavaData(JavaInterface callback); // Define the callback method to receive the JavaData public void receiveJavaData(JavaData data) { Debug.Log("Received Java data: " + data.getIntValue() + ", " + data.getStringValue()); } // Call the JavaInterface to get the data void Start() { getJavaData(this); } } ``` 最后,在Unity项目中创建一个HTML文件,该文件将使用JavaScript调用Java插件和Unity WebGL应用程序: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>Unity WebGL Java Plugin Example</title> <style> #gameContainer { width: 960px; height: 600px; margin: auto; } </style> </head> <body> <div id="gameContainer"></div> <script> // Define the JavaInterface var javaInterface = { getData: function() { // Return a JavaData object return new JavaData(42, "Hello, Java!"); } }; // Load the Unity WebGL application var gameInstance = UnityLoader.instantiate("gameContainer", "Build/UnityWebGL.json"); // Call the JavaPlugin's receiveJavaData method when data is received gameInstance.Module.ccall("receiveJavaData", null, ["number"], [42]); </script> </body> </html> ``` 请注意,此示例仅用于演示目的,实际应用中可能需要更复杂的插件和通信机制。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值