【发布时间】:2016-05-19 06:46:37
【问题描述】:
我不熟悉 Android Studio 上的 HttpUrlConnection。我尝试在 Eclipse 中使用 HttpClient 进行编码,它可以工作。 HttpClient 现在在 Android Studio 中已弃用,因此我尝试使用 HttpUrlConnection 对其进行更改。我在 Android Studio 中的代码是这样的:
public String LedOff(View view) {
try {
URL url = new URL("http://192.168.4.1/LEDOff");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
在 Eclipse 中是这样的:
public void LedOff (View view){
String url = "http://192.168.4.1/LEDOff";
HttpClient httpClient = new DefaultHttpClient();
try {
httpClient.execute(new HttpGet(url));
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
我不知道怎么了。请帮帮我,伙计们。谢谢。
【问题讨论】:
-
请粘贴您的日志..
-
你在代理后面吗?
-
05-19 15:27:12.727 21143-21150/com.ganda.alex.http D/jdwp:handlePacket:cmd=0x1,cmdSet=0xC7,len=0x14,id=0x400067C7,标志=0x0,dataLen=0x9 05-19 15:27:12.727 21143-21150/com.ganda.alex.http D/jdwp:sendBufferedRequest:len=0x34 05-19 15:27:13.228 21143-21150/com.ganda。 alex.http D/jdwp: processIncoming 我的 logcat 就是这样
-
是的,我在代理后面。我正在使用网络服务器来控制 arduino。
标签: android http httpclient httpurlconnection