【发布时间】:2016-05-09 11:39:27
【问题描述】:
我只是想检查用户输入的 url 是否有效到目前为止我尝试过的是 HTTPurl 连接正在得到结果,但响应时间很慢如何让它加快速度让我发布我的代码:
public Boolean Netwrok(String str) {
HttpURLConnection connection = null;
try {
URL myUrl = new URL(str);
connection = (HttpURLConnection) myUrl.openConnection();
connection.setRequestProperty("Accept-Encoding", "identity");
InputStream response = connection.getInputStream();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
connection.disconnect();
}
}
这就是我现在正在尝试做的事情,我正在尝试通过 volley 来获得响应,但不知道如何验证它可以帮助任何人: 让我发布我的截击代码:
RequestQueue queue = Volley.newRequestQueue(getContext());
String str_url = getEditText().getText().toString();
str_url = s;
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, s,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
Intent intent = new Intent(getContext(), LoginActivity.class);
getContext().startActivity(intent);
// mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
setDialogMessage("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
谁能说出我的问题的任何解决方案
【问题讨论】:
标签: android url android-volley httpurlconnection