Android并使用参数调用url

时间:2015-11-12 23:44:24

标签: android

我找不到任何关于如何通过传递一个参数调用简单URL的帮助,不需要返回任何值,只需调用,并且我在服务器上的php将保存传递的参数,例如 http://www.anysitetocall.com?parm1=newval123

请帮我处理工作代码以及我需要的导入,因为我是Android和Java的新手。

2 个答案:

答案 0 :(得分:1)

用于网络请求的漂亮库是Volley。这就是你包含它的方式:

在build.gradle文件的dependency部分中添加它以使用volley

dependencies {
    compile 'com.mcxiaoke.volley:library-aar:1.0.0'
}

它不是官方的,而是官方排球的镜像副本。它会定期与官方的Volley Repository同步和更新,因此您可以毫无顾虑地继续使用它。

https://github.com/mcxiaoke/android-volley

要使用Volley,您必须将android.permission.INTERNET权限添加到您应用的清单中。如果没有这个,您的应用将无法连接到网络。

然后在您加入后,您可以这样做:

final TextView mTextView = (TextView) findViewById(R.id.text);
...

// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://www.anysitetocall.com?parm1=newval123";

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
        new Response.Listener<String>() {
@Override
public void onResponse(String response) {
    // Display the first 500 characters of the response string.
    mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
    mTextView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);

有关如何使用排球的更多信息,请访问:http://developer.android.com/training/volley/simple.html#manifest

来自的答案: Best way to incorporate Volley (or other library) into Android Studio project

http://developer.android.com/training/volley/simple.html#manifest

答案 1 :(得分:0)

我主要使用HttpClient接口,直到它在API级别22中被释放。

现在我正在使用openConnection()。

这是一个如何做到这一点的例子:

URL myURL = new URL("http://www.anysitetocall.com?parm1=newval123"); HttpURLConnection myConn = (HttpURLConnection) myURL.openConnection(); myConn.setRequestMethod("POST");

了解更多信息https://stackoverflow.com/a/29060004/5231413

相关问题