我正在从String变量中的restful api获取数据现在我想转换为JSON对象但是我遇到问题而转换它会引发异常.这是我的代码:
URL url = new URL("SOME URL");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
conn.disconnect();
JSONObject jObject = new JSONObject(output);
String projecname=(String) jObject.get("name");
System.out.print(projecname);
Run Code Online (Sandbox Code Playgroud)
我的字符串包含
{"data":{"name":"New Product","id":1,"description":"","is_active":true,"parent":{"id":0,"name":"All Projects"}}}
Run Code Online (Sandbox Code Playgroud)
这是我想要在json中的字符串,但它在线程"main"中显示我的异常
java.lang.NullPointerException
at java.io.StringReader.<init>(Unknown Source)
at org.json.JSONTokener.<init>(JSONTokener.java:83)
at org.json.JSONObject.<init>(JSONObject.java:310)
at Main.main(Main.java:37)
Run Code Online (Sandbox Code Playgroud) 我是C#的新手,我真的需要帮助.我需要在C#中使用AES-256-CBC加密/解密字符串,我发现这是为了加密字符串:
public static string EncryptString(string message, string KeyString, string IVString)
{
byte[] Key = ASCIIEncoding.UTF8.GetBytes(KeyString);
byte[] IV = ASCIIEncoding.UTF8.GetBytes(IVString);
string encrypted = null;
RijndaelManaged rj = new RijndaelManaged();
rj.Key = Key;
rj.IV = IV;
rj.Mode = CipherMode.CBC;
try
{
MemoryStream ms = new MemoryStream();
using (CryptoStream cs = new CryptoStream(ms, rj.CreateEncryptor(Key, IV), CryptoStreamMode.Write))
{
using (StreamWriter sw = new StreamWriter(cs))
{
sw.Write(message);
sw.Close();
}
cs.Close();
}
byte[] encoded = ms.ToArray();
encrypted = Convert.ToBase64String(encoded);
ms.Close();
}
catch (CryptographicException e)
{
Console.WriteLine("A …Run Code Online (Sandbox Code Playgroud) 我刚开始使用netty,我对他们网站上的文档感到非常失望.
我正在尝试使用Netty连接到URL.我从他们的网站上获取了时间客户端示例并根据我的要求进行了更改.
代码:
public class NettyClient {
public static void main(String[] args) throws Exception {
String host = "myUrl.com/v1/parma?param1=value";
int port = 443;
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ClientHandler());
ch.pipeline().addLast("encoder", new HttpRequestEncoder());
}
});
// Start the client.
ChannelFuture f = b.connect(host, port).sync();
// Wait until the connection is closed.
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
}
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 neo4j-spark-connector 从 Spark 连接到 Neo4j。当我尝试连接到 Neo4j 时遇到身份验证问题org.neo4j.driver.v1.exceptions.AuthenticationException: Unsupported authentication token, scheme='none' only allowed when auth is disabled: { scheme='none' }
我已经检查过并且我传递的凭据是正确的。不知道为什么会失败。
import org.neo4j.spark._
import org.apache.spark._
import org.graphframes._
import org.apache.spark.sql.SparkSession
import org.neo4j.driver.v1.GraphDatabase
import org.neo4j.driver.v1.AuthTokens
val config = new SparkConf()
config.set(Neo4jConfig.prefix + "url", "bolt://localhost")
config.set(Neo4jConfig.prefix + "user", "neo4j")
config.set(Neo4jConfig.prefix + "password", "root")
val sparkSession :SparkSession = SparkSession.builder.config(config).getOrCreate()
val neo = Neo4j(sparkSession.sparkContext)
val graphFrame = neo.pattern(("Person","id"),("KNOWS","null"), ("Employee","id")).partitions(3).rows(1000).loadGraphFrame
println("**********Graphframe Vertices Count************")
graphFrame.vertices.count
println("**********Graphframe Edges Count************")
graphFrame.edges.count
val pageRankFrame = graphFrame.pageRank.maxIter(5).run()
val ranked …Run Code Online (Sandbox Code Playgroud) 我使用以下代码来测试HTML 5的会话存储.它在除IE之外的所有浏览器中都能正常工作.安装的IE版本是10.
代码:
<!DOCTYPE html>
<html>
<head>
<script>
function clickCounter()
{
if(typeof(Storage)!=="undefined")
{
if (sessionStorage.clickcount)
{
sessionStorage.clickcount=Number(sessionStorage.clickcount)+1;
}
else
{
sessionStorage.clickcount=1;
}
document.getElementById("result").innerHTML="You have clicked the button " + sessionStorage.clickcount + " time(s) in this session.";
}
else
{
document.getElementById("result").innerHTML="Sorry, your browser does not support web storage...";
}
}
</script>
</head>
<body>
<p><button onclick="clickCounter()" type="button">Click me!</button></p>
<div id="result"></div>
<p>Click the button to see the counter increase.</p>
<p>Close the browser tab (or window), and try again, and the counter is reset.</p> …Run Code Online (Sandbox Code Playgroud) 我正在使用@valid和@initbinder来验证传递给服务的数据,但我面临的问题@InitBinder是全局工作,即
@InitBinder // possible to leave off for global behavior
protected void initBinder(WebDataBinder binder){
binder.setValidator(new LoginRequestValidator());
}
Run Code Online (Sandbox Code Playgroud)
而不是像我有一个名为LoginRequest的模型对象的特定模型属性:
@InitBinder("LoginRequest") // possible to leave off for global behavior
protected void initBinder(WebDataBinder binder){
binder.setValidator(new LoginRequestValidator());
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,验证器根本没有被调用..这是正确的方法吗?还是我错过了什么?
我能够通过与服务器的连接编写我的http请求但是我无法从服务器读取响应(不确定是否有任何响应)..我如何检查然后读取它?我的服务器返回json作为响应..
客户代码:
public class NettyClient {
public static void main(String[] args) throws Exception {
URI uri = new URI("http://myurl.com/v1/v2?param1=value1");
String scheme = uri.getScheme() == null? "http" : uri.getScheme();
String host = uri.getHost();
int port = 443;
boolean ssl = "https".equalsIgnoreCase(scheme);
// Configure the client.
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.handler(new NettyClientInitializer(false));
// Make the connection attempt.
Channel ch = b.connect(host, port).sync().channel();
// Prepare the HTTP request.
HttpRequest request = new DefaultHttpRequest( …Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的 iPhone 应用程序实现注销功能,该应用程序在客户端使用 jQuery mobile、JS,在服务器端使用 java。目前我要做的是清除cookie并重定向到index.html中的#loginpage标签(我只有1个HTML文件,其中有不同页面的多个标签)。我现在为 clearCookie 所做的是:
Cookie readCookie = null;
for (Cookie cookie : httpRequest.getCookies()) {
if (cookie.getName().equals("CookieForLogin")) {
readCookie = cookie;
break;
}
}
readCookie.setMaxAge(0);
httpResponse.addCookie(readCookie);
Run Code Online (Sandbox Code Playgroud)
但是这段代码没有清除cookie。我已经尝试过 JS 方法,即将到期日期设置为某个以前的日期,在网上给出,但它们也不起作用。另外我没有响应方法HttpServletResponse。如何清除在客户端设置的 cookie 以及如何重定向到特定标签?
我正在使用 Mockito 来模拟HttpServletRequest和HttpServletResponse。我想在我创建的模拟请求中添加 cookie。我怎样才能这样做呢?
我还在服务器端的响应中设置了 cookie。如何从服务器发送的模拟响应中检索 cookie?
我试图使用NVD3绘制线图.但我面临的问题是x轴和y轴的最大值会根据提供的数据自动调整.但我不想要这种默认行为,我想预设X轴和Y轴的最大值.在NVD3中是否可以选择这样做?
我正在编写一个打开txt文件的ac程序,想要读取txt文件的最后一行.我不是那么精通C所以请记住,我可能不知道C中的所有概念.我被困在我使用fscanf读取我的txt文件的所有行的部分,但我想采取最后一行txt文件并获取如下所述的值.
这是我不完整的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
FILE *sync;
void check()
{
int success; //to hold the results if the timestamps match
sync = fopen("database.txt","r");
char file[] = "database.txt";
while (fscanf(sync, "%d.%06d", &file) != EOF)
{
}
fclose(sync);
}
Run Code Online (Sandbox Code Playgroud)
示例txt文件:
/////// / //// ///// ///// //////////////// Time: 1385144574.787665 //////// /
/////// / //// ///// ///// //////////////// Time: 1385144574.787727 //////// /
/////// / //// ///// ///// //////////////// Time: 1385144574.787738 //////// /
/////// / //// ///// ///// //////////////// Time: 1385144574.787746 //////// …Run Code Online (Sandbox Code Playgroud) java ×5
httprequest ×2
json ×2
netty ×2
annotations ×1
apache-spark ×1
c ×1
c# ×1
cookies ×1
cypher ×1
encryption ×1
graph ×1
html5 ×1
httpresponse ×1
jquery ×1
junit ×1
logout ×1
mockito ×1
neo4j ×1
nvd3.js ×1
scala ×1
servlets ×1
session ×1
spring-mvc ×1
validation ×1
web-services ×1