java url。openConnection();实际上没有被调用
我正在为我的学院设计一个毕业设计,主题是“智能家庭,家庭自动化系统”,在一个模型结构上艰难地实现
为了获得完整的图像,Arduaio通过get请求获取pin码,以打开或打开特定家用设备
当我从任何浏览器发送HTTP请求时都很酷,但是当我使用
openConnection();
方法,就好像什么都没发生过一样,但当我用它来获取一些关于家庭房间及其设备的数据时,它工作得非常好
我已经允许该应用程序访问互联网
下面是我为解决这个实际问题而做的一个简单项目的代码:
MainActivity.java
package com.bitsandbytes.xemma_pc.newprototype;
import 安卓.os.AsyncTask;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.view.View;
import 安卓.widget.Button;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new SendRequest().execute();
}
});
}
private static class SendRequest extends AsyncTask <Void,Void,Void>{
@Override
protected Void doInBackground(Void... params) {
HttpURLConnection httpURLConnection = null ;
URL url ;
try {
url = new URL("http://192.168.1.143/pin=13");
httpURLConnection = (HttpURLConnection) url.openConnection();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
httpURLConnection.disconnect();
}
return null;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.bitsandbytes.xemma_pc.newprototype.MainActivity">
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:textAppearance="?安卓:attr/textAppearanceLarge"
安卓:text="Press it to Test it"
安卓:id="@+id/textView"
安卓:layout_marginTop="154dp"
安卓:layout_alignParentTop="true"
安卓:layout_centerHorizontal="true" />
<Button
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="Pin 13"
安卓:id="@+id/button"
安卓:layout_below="@+id/textView"
安卓:layout_centerHorizontal="true" />
</RelativeLayout>
AndroidManifist.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
package="com.bitsandbytes.xemma_pc.newprototype">
<uses-permission 安卓:name="安卓.permission.INTERNET"/>
<application
安卓:allowBackup="true"
安卓:icon="@mipmap/ic_launcher"
安卓:label="@string/app_name"
安卓:supportsRtl="true"
安卓:theme="@style/AppTheme">
<activity 安卓:name=".MainActivity">
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我提前感谢你的帮助和提示
EDIT:谢谢你们的帮助,但它真正帮助我的是使用getResponseMessage()方法,它非常有效:D 我不需要使用。之前,connect方法获取了一些JSON字符串,但它是有效的。 不管怎样,谢谢你的帮助
# 1 楼答案
openConnection
只是解析URL并创建相应的URLConnection
子类。网络I/O只有在您发送或接收某些信息时才会发生# 2 楼答案
URL.openConnection()
只向指定的资源返回URLConnection
您可以通过调用该特定
URLConnection
的connect()
方法来打开与该资源的实际连接例如:
需要连接的操作(如
getInputStream()
或getOutputStream()
)将隐式调用connect()
,因此不需要在此类操作之前显式调用它: