HTTP帖子与Matlab [英] HTTP Post with Matlab

查看:120
本文介绍了HTTP帖子与Matlab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照这篇文章中给出的示例:如何发送数据从Matlab到Rails ,但收到的错误消息是我找不到任何信息。我的脚本如下所示:

I followed the example given in this post: How to send data from Matlab to Rails, but am receiving an error message that I cannot find any information on. My script looks like this:

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@aol.com,""password:""password""}"}');
httppost.setEntity(tokenRequest);

response = httpclient.execute(httppost);

在最后一行,我收到错误:

On the last line, I get the error:

发生Java异常:java.lang.VerifyError:无法从最终类继承

从在线搜索中,我认为这是软件版本问题。我尝试使用这些文件的4.2版本(与其他帖子中使用的相同),但我收到了同样的错误。有谁知道什么可能是错的?或者知道如何在不使用这些外部库的情况下做我想做的事情?

From searches online, I gather that this is a software version issue. I tried using the 4.2 builds of those files (the same ones used in the other post), but I received the same error. Does anyone have an idea what could be wrong? Or know a way to do what I am trying to do without using these external libraries?

编辑:

最初我尝试使用此代码:

originally I tried using this code:

tokenRequest = {'session', '{''email_address'':''email@aol.com'',''password'':''password''}'};
token = urlread('http://localhost:3000/signin','POST',tokenRequest);

但是我收到NoMethodError导致我发到其他帖子:

but I received the NoMethodError that led me to that other post:

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

我认为抛出此错误的原因是因为服务器认为它正在接收一个String对象,该对象没有每个方法。我假设我会通过使用'Content-Type'参数来指定它的json来解决这个问题。有没有办法使用urlread这样做?

I think the reason it throws this error is because the server thinks it is receiving a String object, which doesn't have an each method. I assume I would fix this by using the 'Content-Type' argument to specify that its json. Is there a way to do this using urlread?

编辑:java libs问题的完整堆栈跟踪

full stack trace for java libs issue

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)


推荐答案

urlread2 原来是一个更好的解决方案。此代码完成工作:

urlread2 turned out to be a much better solution. This code does the job:

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

这篇关于HTTP帖子与Matlab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆