我想将数据从java程序发送到我目前通过本地xampp服务器托管的mysql服务器。由于这个java类将被用android(也是swift)编写的代码使用它不会被使用jdbc驱动程序(至少没有头疼),因此我试图在数据的帮助下将数据传递给php服务器java POST。
问题是,当我运行代码时,字面上没有任何反应。没有错误,没有数据添加到SQL,没有任何事情发生。所以也许比一个更好的程序员可以查看我的代码,也许看到我目前无法解决的问题!
Java代码:
package::functionname
PHP
public static String main(String x, String y){
try{
String username = x;
String password = y;
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"
+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
// LÄSER FRÅN HEMSIDAN
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine()) != null){
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
}catch(Exception e){
e.printStackTrace();
}
return null;
}
我真的可以在这里使用一些帮助!! :)