0

我正在使用脚本尝试将 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")+
    "&param2="+URLEncoder.encode("value2","UTF-8")+
    "&param3="+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();
    }
} 

谢谢!

4

1 回答 1

2

您已将变量声明为,url但您正尝试将其用作loginUrl.

在您的代码中检查这些声明和初始化

    URL url;
    url=new URL("http://mysite/test.php");

当您尝试打开连接时出现问题:

conn=(HttpURLConnection)loginUrl.openConnection();

它应该是

conn=(HttpURLConnection)url.openConnection();
于 2013-07-12T17:12:54.737 回答