我可以在浏览器中打开php文件,但android说找不到该文件
我是编程新手,所以如果这是一个愚蠢的错误,我会提前道歉。我在服务器上有一个名为“info.PHP”的PHP文件,通过导航到138.68.176.180/info.PHP,我可以在浏览器中毫无问题地打开它。但当我尝试从android应用程序向此文件发送数据时,它会给我以下错误: java.io.FileNotFoundException: 我的代码是:我可以在浏览器中打开php文件,但android说找不到该文件,php,android,Php,Android,我是编程新手,所以如果这是一个愚蠢的错误,我会提前道歉。我在服务器上有一个名为“info.PHP”的PHP文件,通过导航到138.68.176.180/info.PHP,我可以在浏览器中毫无问题地打开它。但当我尝试从android应用程序向此文件发送数据时,它会给我以下错误: java.io.FileNotFoundException: 我的代码是: @Override protected String doInBackground(String... params) {
@Override
protected String doInBackground(String... params) {
URL url;
String urlParams = params[0];
String targetURL="http://138.68.176.180/info.php";
HttpURLConnection connection = null;
try {
//Create connection
url = new URL(targetURL);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" +
Integer.toString(urlParams.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream(
connection.getOutputStream());
wr.writeBytes(urlParams);
wr.flush();
wr.close();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
connection.disconnect();
return response.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
我在服务器端做错了什么?还是在代码方面
谢谢 使用此代码检索数据,在字符串行中,您的数据将作为完整字符串追加,如浏览器中所示
public void getData(){
class GetDataJSON extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://138.68.176.180/info.php");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
@Override
protected void onPostExecute(String result){
myJSON=result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
public void getData(){
类GetDataJSON扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…参数){
DefaultHttpClient httpclient=新的DefaultHttpClient(新的BasicHttpParams());
HttpPost HttpPost=新的HttpPost(“http://138.68.176.180/info.php");
//取决于您的web服务
setHeader(“内容类型”、“应用程序/json”);
InputStream InputStream=null;
字符串结果=null;
试一试{
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
inputStream=entity.getContent();
//默认情况下,json是UTF-8
BufferedReader=新的BufferedReader(新的InputStreamReader(inputStream,“UTF-8”),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null)
{
sb.追加(第+行“\n”);
}
结果=sb.toString();
}捕获(例外e){
//哎呀
}
最后{
尝试{if(inputStream!=null)inputStream.close();}捕获(异常挤压){}
}
返回结果;
}
@凌驾
受保护的void onPostExecute(字符串结果){
myJSON=结果;
showList();
}
}
GetDataJSON g=新的GetDataJSON();
g、 执行();
}
请不要提及与此无关的Android Studio。你说的是你的Andtoid应用程序。请更正您的帖子。谢谢@greenapps-我删除了android studio标记,也请更改主题。谢谢Arun,我将尝试更多使用JSON的方法。Instedi仍然无法将数据发送到php文件,我正在试图找出我做错了什么…@fjvs0283您只需尝试在文本视图中显示该行,这样您就可以再次看到输出了@Arun Mohan,但我现在还不能投票,我没有代表@fjvs0283没关系,你只要接受答案,你有接受答案的条件