【问题标题】:HTTP Get Not Working [duplicate]HTTP 无法正常工作 [重复]
【发布时间】:2016-05-06 12:11:27
【问题描述】:

编辑:我非常抱歉一开始我问的问题很糟糕,我非常沮丧和疲倦,但我知道这真的不是借口。无论如何,我编辑了它。我也已经在 StackOverflow 论坛和其他论坛上看了很多,并尝试了他们的方法,但没有结果,所以我想这不是一个重复的问题。

经过数小时的尝试和使用多种方法后,我终于放弃了,并决定在这里发布问题。太令人沮丧了,我无法让它工作,这是一个家庭自动化项目,在这个项目上学习像 C 和 python 这样的新语言从来都不是问题,所以不能让它工作是非常令人沮丧的。无论如何这里是代码:

public void buttonOnClick(View v) {
    Button button = (Button) v;
    ((Button) v).setText("clicked");
        // The request here
        // The request here
        // The request here
        // The request here

        }

这是一个带有一个按钮的简单应用程序,它应该向 192.168.0.150/main_light/switch 发送一个简单的 http get 请求。我知道这应该可以工作,因为我一直在 Tasker 中为此使用 HTTP GET(并且它在 Python 中工作)。我真的希望有人可以在这里帮助我,谢谢!

附:我知道这里还有一些无用的操作,我是一个 JAVA 菜鸟,并决定在我最终能够正常工作之前不要搞砸任何事情。

【问题讨论】:

  • 与其发布大量与触发 HTTP GET 请求无关的代码,不如展示您尝试过哪些代码来触发请求。
  • 从这里尝试答案make an http request with android
  • @JanusVarmarken 非常抱歉,我已经编辑了问题。

标签: java android http-get


【解决方案1】:

使用HttpURLConnection 来实现同样的效果:

String serverURL = "http://192.168.0.150/main_light/switch";
URL url = new URL(serverURL);
HttpURLConnection connection = null;
try {
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod("GET");
    BufferedReader in = new BufferedReader(
    new InputStreamReader(con.getInputStream()));
    //Do something with this InputStream
    // handle the response
    int status = connection.getResponseCode();
    // If response is not success
    if (status != 200) {
        throw new IOException("Post failed with error code " + status);
    }
} catch (Exception e) {
    Log.e(TAG,e.toString());
} finally {
     if(connection != null)
         connection.disconnect();
}

希望这会有所帮助!

【讨论】:

  • 当然这无济于事,因为您没有阅读服务器发送的页面。所以你的代码gets 什么都没有。
  • 糟糕,错过了最重要的事情。感谢您指出。
  • 您的代码仍然无法检索到该页面。
  • 我得到一个 ANDROID.OS.NETWORKONMAINTHREADEXCPETION 这可能意味着我应该将它作为异步任务运行?
  • 我使用异步任务让它工作,真的很愚蠢,我以前没有想到它,因为我已经尝试过这个确切的方法。无论如何,非常感谢回答我的问题,考虑到它被问得有多糟糕。祝你有美好的一天!
【解决方案2】:

使用这个方法:

public static int get(String url) throws Exception {
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");
    int responseCode = con.getResponseCode();
    BufferedReader in = new BufferedReader(
        new InputStreamReader(con.getInputStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null) {
        //consume response if any;
    }
    in.close();
    return responseCode;
}

如果需要,实现请求的消费。

【讨论】:

    【解决方案3】:

    尝试使用http://square.github.io/okhttp/

    把代码放在这里:

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               OkHttpClient client = new OkHttpClient();
               String run(String url) throws IOException {
               Request request = new Request.Builder()
                   .url(url)
                   .build();
    
               Response response = client.newCall(request).execute();
               return response.body().string();
            }
        }
    });
    

    【讨论】:

      猜你喜欢
      • 2017-07-15
      • 2015-09-06
      • 2016-05-12
      • 2018-07-19
      • 2018-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多