【问题标题】:HttpUrlConnection in Android no error but not workingAndroid中的HttpUrlConnection没有错误但不起作用
【发布时间】: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


【解决方案1】:

确保您在 Manifest 中拥有这两个权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

编辑
LedOff() 方法添加了代码,将成功的响应视为String

试试这个,根据Android Docs

public String LedOff(View view) {
    try {
        URL url = new URL("http://192.168.4.1/LEDOff");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000 /* milliseconds */);
        conn.setConnectTimeout(15000 /* milliseconds */);
        conn.setRequestMethod("GET"); // Or any method you need
        conn.setDoInput(true);

        // Starts the query
        conn.connect();
        int response = conn.getResponseCode();
        Log.d(DEBUG_TAG, "The response code is: " + response);

        if ((response >= 200) && (response < 300)) {
            // We are assuming here that whatever the response is, it can be parsed as a String
            Log.d(DEBUG_TAG, "The response is: " + conn.getResponseMessage());
        }
    } catch(Exception ex) {
        // Handle any exceptions
    }
}

【讨论】:

  • 能否给我一个回应的例子。我很抱歉,因为我只是这里的新手。这是我第一次设置 http 连接。
  • 您希望通过连接到“192.168.4.1/LEDOff”实现什么目标?
  • 我已经编辑了我的答案并包含了将成功响应视为String的代码。
  • 好吧 192.168.4.1/LedOff 实际上会关闭 LED。 LED 已经在 arduino 中编程,所以我决定只获取 url,然后将其放入 arduino 中以便于编程。
  • Android -> WebServer -> Arduino 类似这样的
【解决方案2】:

您必须通过调用getInputStream()getResponseCode()getResponseMessage() 来提交请求,以便返回和处理响应。

Connect() 方法只是创建连接。

检查:

Log.i("Response Code" , urlConnection .getResponseCode()); 

看看你得到了什么回应。

【讨论】:

    【解决方案3】:

    这样试试

        URL url = new URL("http://192.168.4.1/LEDOff");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(true)
        urlConnection.connect();
        if(urlConnection.getResponseCode()==200)
        {
         ...
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-01
      • 1970-01-01
      相关资源
      最近更新 更多