2015-05-01 32 views
0

我正在嘗試使用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)。

+0

什麼樣的例外?在哪個catch塊?由哪個語句引起的? – greenapps

+0

java.io.filenotfoundexception –

+0

是不是有三個問題? – greenapps

回答

1

,而不是禁用應用程序\ HTTP \中間件\ VerifyCsrfToken.php你可以試着將文件更新到類似的:

class VerifyCsrfToken extends BaseVerifier { 

    private $openRoutes = 
    [ 
     'public/login', 
    ]; 

    public function handle($request, Closure $next) 
    { 
     foreach($this->openRoutes as $route) 
     { 
      if ($request->is($route)) 
      { 
       return $next($request); 
      } 
     } 

     return parent::handle($request, $next); 
    } 
}; 

這可以讓你明確地繞過你不想沒有禁用CSRF驗證具體路線全球驗證。我用它來做我的ajax調用。

+0

感謝以下代碼片段。但它不適合我。 –

+0

好的。它的工作。有一個問題,我的文件或理解:)無論如何,有沒有辦法讓android應用程序的csrf身份驗證以及? casue我會在應用程序的各種表格上使用很多crud操作。 –

+0

太棒了!真高興你做到了。 – aethergy