我正在使用此脚本尝试将 POST 数据从我的 Android 应用程序发送到 PHP 脚本。但由于错误“loginUrl 无法解析”,它甚至不允许我运行它。由于其他人似乎已经让这段代码工作了,我在这里错过了一些明显的东西吗?这是代码,但由我更改(通过上面的链接查看注释):
public void postData() {
URL url;
HttpURLConnection conn;
try{
url=new URL("http://mysite/test.php");
String param="param1=" + URLEncoder.encode("value1","UTF-8")+
"¶m2="+URLEncoder.encode("value2","UTF-8")+
"¶m3="+URLEncoder.encode("value3","UTF-8");
conn=(HttpURLConnection)loginUrl.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setFixedLengthStreamingMode(param.getBytes().length);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
PrintWriter out = new PrintWriter(conn.getOutputStream());
out.print(param);
out.close();
String response= "";
Scanner inStream = new Scanner(conn.getInputStream());
while(inStream.hasNextLine())
response+=(inStream.nextLine());
}
catch(MalformedURLException ex){
Toast.makeText(GameButton.this, ex.toString(), 1 ).show();
}
catch(IOException ex){
Toast.makeText(GameButton.this, ex.toString(), 1 ).show();
}
}
谢谢!