【问题标题】:Send GET Request发送 GET 请求
【发布时间】:2016-04-11 18:50:31
【问题描述】:

我知道有很多这样的主题,我疯狂地搜索并阅读了其中的大部分内容,但找不到真正对我有帮助且有效的答案。

我想要做的很简单,我只想使用 GET 发送 2 个值并将它们保存在 MySql 中。

我已经创建了 PHP 文件并且正在运行(我在 chrome 中手动测试了它),并且我在 Android Studio 中有这段代码:

try {
        URL url = new URL("http://myWebsite.com/connection.php");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestProperty("value1","blabla");
        urlConnection.setRequestProperty("value2","blabla");

    } catch(Exception e){
    Log.d("Exception",e.toString());
    }

我不知道还可以尝试什么,这样的事情应该很简单,但我不能只是找到解决方案。

谢谢!!

编辑: 我也试过了:

try {
        URL url = new URL("http://myWebsite.com/connection.php?value1=blabla&value2=blubla");
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

    } catch(Exception e){
    Log.d("Exception",e.toString());
    }

编辑 2: 将此添加到我的代码中:

private class MyTask extends AsyncTask<Void, Void, Void> {


        @Override
        protected Void doInBackground(Void... voids) {
            try {
                URL url = new URL("http://mySite.tk/connection.php?value1=blabla&value2=blubla");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                Log.d("Progress","Sending..");
                urlConnection.connect();
                Log.d("Progress","Sent!");

            } catch(Exception e){
                Log.d("Message",e.toString());
            }
            return null;
        }
    }

现在我在单击按钮时调用“new MyTask().execute()”,但不起作用:(

【问题讨论】:

    标签: php android get


    【解决方案1】:

    您可以使用名为 Volley 的 Google 库轻松发送 GET 请求,该库专为 Android 上的请求而设计

    我建议您考虑使用 Volley 来处理您的未来请求,这里是一个 GET 请求示例:

    final String url = "http://yourScript.com/scipt.php";
    
    // prepare the Request
    JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, url, null,
        new Response.Listener<JSONObject>() 
        {
            @Override
            public void onResponse(JSONObject response) {   
                            // display response     
                Log.d("Response", response.toString());
            }
        }, 
        new Response.ErrorListener() 
        {
             @Override
             public void onErrorResponse(VolleyError error) {            
                Log.d("Error.Response", response);
           }
        }
    ){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<>();
                map.put("parameter1", param1);
                map.put("parameter2", param2); 
    
                return map;
            }
        };
    
    // add it to the RequestQueue   
    queue.add(getRequest);
    

    【讨论】:

    • 这有帮助,我添加了代码并修改为我需要的内容,只需下载 volley.jar 并将其插入我项目的 libs 文件夹中。惊人的!谢谢!!我唯一不确定(但正在工作)是我在这里发送 get 请求 final String url = "yourScript.com/scipt.php?value=vala&value2=vala2";这是它的工作方式吗?还是应该在 HashMap 中发送请求?
    • 其实我不知道,因为我通常使用 Post 请求!在您的 php 脚本 (if(isset($_GET['parameter1']))) 中尝试两种方法
    • 哦,好吧,是的,我认为 HashMap 仅用于发布请求,实际上更安全。再次感谢您!
    • 好吧,我不知道!不客气,很高兴能帮到你! :)
    【解决方案2】:

    获取值不会作为属性发送。它们作为 URL 的一部分发送。 http://example.com/myurl?variable1=value1&variable2=value2

    【讨论】:

    • 你是对的,在更改为“setRequestProperty”之前也尝试过。
    • 好吧,您确实做了 urlConnection .connect 并实际读取了结果,对吧?我以为您只是在发送变量时遇到了问题。上面的代码创建了对象,它实际上还没有访问你的网络服务器。
    • 我刚刚添加了 urlConnection.connect();在 Try{} 的最后一行上方。现在我收到这个错误:android.os.NetworkOnMainThreadException。
    • 你不能在主线程上联网。您必须在另一个线程或异步任务上执行此操作。但是您现在正尝试访问您的网络服务器,因此您走在正确的道路上 - 在您没有实际发送请求之前。
    • 太棒了!谢谢你!刚刚在 StackOverflow 中找到了一些关于此的内容,添加这应该会有所帮助,对吗? StrictMode.ThreadPolicy 策略 = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy);
    猜你喜欢
    • 2016-04-18
    • 1970-01-01
    • 2020-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    相关资源
    最近更新 更多