我正在嘗試使用HttpUrlConnection發佈到Laravel。但正如標題所示,它正在拋出異常。HttpUrlConnection發佈到Laravel - java.io.filenotfoundexception
請檢查我的代碼,讓我知道,我錯了。
public String HttpUrlPost()
{
BufferedWriter writer;
BufferedReader reader;
StringBuilder sb= new StringBuilder();
String data;
try
{
URL url = new URL("http://192.168.0.104/laravel5/public/login");
Log.d(TAG,"URL is : " + url.toString());
data = URLEncoder.encode("username",HTTP.UTF_8) + "=" + URLEncoder.encode("me",HTTP.UTF_8);
data += "&" + URLEncoder.encode("password",HTTP.UTF_8) + "=" + URLEncoder.encode("admin",HTTP.UTF_8);
Log.d(TAG,"data is : " + data);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
OutputStream os = connection.getOutputStream();
writer = new BufferedWriter(new OutputStreamWriter(os, HTTP.UTF_8));
writer.write(data);
writer.flush();
reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null)
{
sb.append(line);
}
writer.close();
reader.close();
}
catch(IOException e)
{
e.printStackTrace();
}
return sb.toString();
}
拉拉維的一面很簡單。我只是想檢查是否返回任何東西。
Route::post('login',function(){
$users = array('username' => 'a', 'password'=>'d');
return $users;});
請注意,我可以很容易地使用get方法與laravel(它返回Json)。
什麼樣的例外?在哪個catch塊?由哪個語句引起的? – greenapps
java.io.filenotfoundexception –
是不是有三個問題? – greenapps