我已经将一些.php文件上传到我的服务器,该服务器使用cPanel连接到存储在同一服务器上的MySQL数据库。这些文件在public_html / myfiles上。然后我想我的文件URL是" www.myserver.com/myfiles/file2.php"
我试图通过以下代码确定我的Android应用是否可以找到它:
public boolean exists(String URLName){
try {
HttpURLConnection con =
(HttpURLConnection) new URL(URLName).openConnection();
con.setFollowRedirects(false);
con.setInstanceFollowRedirects(false);
con.setRequestMethod("HEAD");
con.setDoOutput(false);
return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
}
catch (Exception e) {
e.printStackTrace();
return false;
}
}
URLName =" www.myserver.com/myfiles/file2.php"
此方法返回false。
我也尝试使用以下代码使用Volley:
private void getData() {
String id = "1";
if (id.equals("")) {
Toast.makeText(getContext(), "Please enter an id", Toast.LENGTH_LONG).show();
return;
}
String url = "www.myserver.com/myfiles/file2.php?id="+id;
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
Log.d("VOLLEY RESPONSE",s);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.d("VOLLEY ERROR","Error");
}
});
RequestQueue requestQueue = Volley.newRequestQueue(getContext());
requestQueue.add(stringRequest);
}
但我得到的VolleyError并没有提供太多信息..它只返回500&#34;服务器错误&#34;。
我该怎么做?或者我做错了什么?
答案 0 :(得分:0)
如果是Volley,你可以试试这个:
URL = String.format("http://www.example.com/test/?val_1=%1$s&val_2=%2$s",first,second);
答案 1 :(得分:0)
使用 JsonObjectRequest alogn与此网址....
final String url = "http://httpbin.org/get?param1=hello";
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);
}
});
queue.add(getRequest);
同时检查this
答案 2 :(得分:0)
问题解决了。
这些文件的.php代码存在问题(我没有写'?&gt;'来关闭文件),与Android Java编码无关。它与JsonObjectRequest和StringRequest一起工作正常。
答案 3 :(得分:0)
我遇到了同样的问题并对GET
和php
代码使用了java
,然后按预期得到了响应。
StringRequest strObjRequest = new StringRequest(Request.Method.GET,
"http://www.mpworld.in/test.php?name="+trainNumber+"&email="+date+"&question="+date,
listener, errorListener)