通过URL调用Java中的PHP文件
我正在尝试开发一个android应用程序,它调用一个php文件来查询和从数据库中提取数据。这个URL可以在我的手机上通过网络浏览器访问,但我似乎无法通过下面的java代码调用它。有人能帮我从Java代码中调用PHP文件吗通过URL调用Java中的PHP文件,java,php,android,Java,Php,Android,我正在尝试开发一个android应用程序,它调用一个php文件来查询和从数据库中提取数据。这个URL可以在我的手机上通过网络浏览器访问,但我似乎无法通过下面的java代码调用它。有人能帮我从Java代码中调用PHP文件吗 URL url = new URL("http://10.0.3.2/MYCODE/app/login.php"); String urlParams = "name="+name+"&password="+passwor
URL url = new URL("http://10.0.3.2/MYCODE/app/login.php");
String urlParams = "name="+name+"&password="+password;
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setDoOutput(true);
OutputStream os = httpURLConnection.getOutputStream();
os.write(urlParams.getBytes());
os.flush();
os.close();
InputStream is = httpURLConnection.getInputStream();
while((tmp=is.read())!=-1){
data+= (char)tmp;
}
is.close();
httpURLConnection.disconnect();
我得到以下错误:java.io.FileNotFoundException:
我使用的是
POST
方法,因为它更安全。请确保清单和WIFI中的以下两行已启用并连接到子网10.0.3.x
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
如果post方法为get,则URL中的“?”在哪里?请确保清单和WIFI中的以下两行已启用并连接到子网10.0.3.x
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
如果post方法是get,那么URL中的“?”在哪里?您可以在此方法中使用
URL URL=newURL(“http://localhost/test.php?print=success");
URLConnection=url.openConnection();
connection.connect()代码>
同样的,我在我的一个项目中使用了这个clueshop.com你可以在这个方法中使用URL URL=newurl(“http://localhost/test.php?print=success");
URLConnection=url.openConnection();
connection.connect()代码>
同样,我在我的一个项目中使用了这个clueshop.com如果它是GET方法,那么我们可以使用下面的代码行,调用文件的问题可能是.php文件路径不正确
//Integrating url with values [Starts]
Map<String, String> request = new HashMap<String, String>();
request.put("name", name);
request.put("password", password);
Uri.Builder uriBuilder = new Uri.Builder();
uriBuilder.encodedPath("http://10.0.3.2/MYCODE/app/login.php");
if (mapOfStrings != null) {
for (Map.Entry<String, String> entry : request.entrySet()) {
Log.d("buildSanitizedRequest", "key: " + entry.getKey()
+ " value: " + entry.getValue());
uriBuilder.appendQueryParameter(entry.getKey(),
entry.getValue());
}
}
String uriString;
try {
uriString = uriBuilder.build().toString(); // May throw an
// UnsupportedOperationException
} catch (Exception e) {
Log.e("Exception", "Exception" + e);
}
//Integrating url with values [Ends]
HttpURLConnection connection = null;
try {
URL url = new URLuriBuilder.build().toString());
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Accept-Charset", "utf-8,*");
Log.d("Get-Request", url.toString());
try {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
bufferedReader.close();
Log.d("Get-Response", stringBuilder.toString());
return new JSONObject(stringBuilder.toString());
} finally {
connection.disconnect();
}
} catch (Exception e) {
Log.e("ERROR", e.getMessage(), e);
return null;
}`
//将url与值集成[开始]
Map request=newhashmap();
请求。输入(“名称”,名称);
请求。输入(“密码”,密码);
Uri.Builder uriBuilder=新的Uri.Builder();
uriBuilder.encodedPath(“http://10.0.3.2/MYCODE/app/login.php");
if(mapOfStrings!=null){
for(Map.Entry:request.entrySet()){
Log.d(“buildSanitizedRequest”,“key:+entry.getKey()
+“值:”+entry.getValue());
uriBuilder.appendQueryParameter(entry.getKey(),
entry.getValue());
}
}
字符串uriString;
试一试{
uriString=uriBuilder.build().toString();//可能会引发
//不支持操作异常
}捕获(例外e){
Log.e(“例外”、“例外”+e);
}
//将url与值集成[结束]
HttpURLConnection=null;
试一试{
URL=new URLuriBuilder.build().toString());
connection=(HttpURLConnection)url.openConnection();
setRequestProperty(“内容类型”、“应用程序/json”);
setRequestProperty(“接受”、“应用程序/json”);
setRequestProperty(“接受字符集”,“utf-8,*”);
Log.d(“获取请求”,url.toString());
试一试{
BufferedReader BufferedReader=新的BufferedReader(
新的InputStreamReader(connection.getInputStream());
StringBuilder StringBuilder=新的StringBuilder();
弦线;
而((line=bufferedReader.readLine())!=null){
stringBuilder.append(行).append(“\n”);
}
bufferedReader.close();
Log.d(“获取响应”,stringBuilder.toString());
返回新的JSONObject(stringBuilder.toString());
}最后{
连接断开();
}
}捕获(例外e){
Log.e(“错误”,e.getMessage(),e);
返回null;
}`
如果是GET方法,那么我们可以使用下面几行代码,调用文件的问题可能是.php文件路径不正确
//Integrating url with values [Starts]
Map<String, String> request = new HashMap<String, String>();
request.put("name", name);
request.put("password", password);
Uri.Builder uriBuilder = new Uri.Builder();
uriBuilder.encodedPath("http://10.0.3.2/MYCODE/app/login.php");
if (mapOfStrings != null) {
for (Map.Entry<String, String> entry : request.entrySet()) {
Log.d("buildSanitizedRequest", "key: " + entry.getKey()
+ " value: " + entry.getValue());
uriBuilder.appendQueryParameter(entry.getKey(),
entry.getValue());
}
}
String uriString;
try {
uriString = uriBuilder.build().toString(); // May throw an
// UnsupportedOperationException
} catch (Exception e) {
Log.e("Exception", "Exception" + e);
}
//Integrating url with values [Ends]
HttpURLConnection connection = null;
try {
URL url = new URLuriBuilder.build().toString());
connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Accept-Charset", "utf-8,*");
Log.d("Get-Request", url.toString());
try {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
bufferedReader.close();
Log.d("Get-Response", stringBuilder.toString());
return new JSONObject(stringBuilder.toString());
} finally {
connection.disconnect();
}
} catch (Exception e) {
Log.e("ERROR", e.getMessage(), e);
return null;
}`
//将url与值集成[开始]
Map request=newhashmap();
请求。输入(“名称”,名称);
请求。输入(“密码”,密码);
Uri.Builder uriBuilder=新的Uri.Builder();
uriBuilder.encodedPath(“http://10.0.3.2/MYCODE/app/login.php");
if(mapOfStrings!=null){
for(Map.Entry:request.entrySet()){
Log.d(“buildSanitizedRequest”,“key:+entry.getKey()
+“值:”+entry.getValue());
uriBuilder.appendQueryParameter(entry.getKey(),
entry.getValue());
}
}
字符串uriString;
试一试{
uriString=uriBuilder.build().toString();//可能会引发
//不支持操作异常
}捕获(例外e){
Log.e(“例外”、“例外”+e);
}
//将url与值集成[结束]
HttpURLConnection=null;
试一试{
URL=new URLuriBuilder.build().toString());
connection=(HttpURLConnection)url.openConnection();
setRequestProperty(“内容类型”、“应用程序/json”);
setRequestProperty(“接受”、“应用程序/json”);
setRequestProperty(“接受字符集”,“utf-8,*”);
Log.d(“获取请求”,url.toString());
试一试{
BufferedReader BufferedReader=新的BufferedReader(
新的InputStreamReader(connection.getInputStream());
StringBuilder StringBuilder=新的StringBuilder();
弦线;
而((line=bufferedReader.readLine())!=null){
stringBuilder.append(行).append(“\n”);
}
bufferedReader.close();
Log.d(“获取响应”,stringBuilder.toString());
返回新的JSONObject(stringBuilder.toString());
}最后{
连接断开();
}
}捕获(例外e){
Log.e(“错误”,e.getMessage(),e);
返回null;
}`
我看不出这是如何回答问题的。我看不出这是如何回答问题的。如果您对回答不感兴趣,请不要发布问题。如果您对回答不感兴趣,请不要发布问题。