2013-01-08 138 views
1

我按照這篇文章給出的例子:How to send data from Matlab to Rails,但我收到一條錯誤消息,我找不到任何信息。我的腳本是這樣的:HTTP Post with Matlab

javaaddpath('./httpcomponents/httpclient-4.2.2.jar') 
javaaddpath('./httpcomponents/httpcore-4.2.2.jar') 

import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.StringEntity; 

httpclient = DefaultHttpClient(); 

httppost = HttpPost('http://localhost:3000/signin'); 
httppost.addHeader('Content-Type','application/json'); 
httppost.addHeader('Accept','application/json'); 

tokenRequest = StringEntity('{"session", "{email_address:""[email protected],""password:""password""}"}'); 
httppost.setEntity(tokenRequest); 

response = httpclient.execute(httppost); 

就上線,我得到的錯誤:發生

Java異常:java.lang.VerifyError的:不能從最後一類

從搜索繼承在線,我收集這是一個軟件版本問題。我嘗試使用這些文件的4.2版本(與其他文章中使用的版本相同),但我收到了同樣的錯誤。有沒有人有一個想法可能是錯的?或者知道一種方法來做我想做的事情,而不使用這些外部庫?

編輯:

本來我嘗試使用此代碼:

tokenRequest = {'session', '{''email_address'':''[email protected]'',''password'':''password''}'}; 
token = urlread('http://localhost:3000/signin','POST',tokenRequest); 

,但我收到,導致我的其他職位的NoMethodError:

NoMethodError (undefined method `each' for "{'email_address':'[email protected]','password':'password'}":String): 
    app/models/session.rb:14:in `initialize' 

我想原因它拋出此錯誤是因爲服務器認爲它正在接收一個沒有每個方法的String對象。我假設我會通過使用'Content-Type'參數來指定它的json來解決這個問題。有沒有辦法使用urlread來做到這一點?

編輯:對Java庫問題

Java exception occurred: 
java.lang.VerifyError: Cannot inherit from final class 

at java.lang.ClassLoader.defineClass1(Native Method) 

at java.lang.ClassLoader.defineClass(Unknown Source) 

at java.security.SecureClassLoader.defineClass(Unknown Source) 

at java.net.URLClassLoader.defineClass(Unknown Source) 

at java.net.URLClassLoader.access$000(Unknown Source) 

at java.net.URLClassLoader$1.run(Unknown Source) 

at java.security.AccessController.doPrivileged(Native Method) 

at java.net.URLClassLoader.findClass(Unknown Source) 

at com.mathworks.jmi.CustomURLClassLoader.findClass(ClassLoaderManager.java:760) 

at java.lang.ClassLoader.loadClass(Unknown Source) 

at java.lang.ClassLoader.loadClass(Unknown Source) 

at java.lang.ClassLoader.loadClassInternal(Unknown Source) 

at org.apache.http.impl.client.DefaultHttpClient.createHttpParams(DefaultHttpClient.java:157) 

at org.apache.http.impl.client.AbstractHttpClient.getParams(AbstractHttpClient.java:448) 

at org.apache.http.impl.client.AbstractHttpClient.createClientConnectionManager(AbstractHttpClient.java:309) 

at org.apache.http.impl.client.AbstractHttpClient.getConnectionManager(AbstractHttpClient.java:466) 

at org.apache.http.impl.client.AbstractHttpClient.createHttpContext(AbstractHttpClient.java:286) 

at 
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:851) 

at 
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:805) 

at 
org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:784) 
+0

給我們完整的堆棧跟蹤 – Archer

回答

0

urlread2竟然是一個更好的解決方案。此代碼完成這項工作:

tokenRequest = '{ "session" : { "email_address": "[email protected]", "password": "password" } }'; 
header = http_createHeader('Content-Type','application/json'); 
token = urlread2('http://localhost:3000/signin.json','POST',tokenRequest,header); 
1

完整的堆棧跟蹤看起來正在發送的數據沒有被正確引用。最終結果應該是

{'email_address':'[email protected]','password':'password'} 

嘗試改變代碼

tokenRequest = StringEntity('{"session", {"email_address":"[email protected]","password":"password"}}'); 
+0

是的,這將是一個問題。但在我使用DefaultHttpClient的情況下,問題是它沒有向服務器發送任何東西 - java異常停止它。我重寫了其他腳本以匹配您發佈的最終結果,但仍然收到NoMethodError。 – Cbas