我对 Java 8 lambdas 和其他东西很陌生……我想编写一个 lambda 函数,它接受 a JSONArray,遍历它的JSONObjects 并创建某个字段的值列表。
例如,一个函数接受JSONArray:[{name: "John"}, {name: "David"}]并返回一个列表["John", "David"]。
我写了以下代码:
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class Main {
public static void main(String[] args) {
JSONArray jsonArray = new JSONArray();
jsonArray.add(new JSONObject().put("name", "John"));
jsonArray.add(new JSONObject().put("name", "David"));
List list = (List) jsonArray.stream().map(json -> json.toString()).collect(Collectors.toList());
System.out.println(list);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我收到一个错误:
线程“main”中的异常 java.lang.NullPointerException
你知道如何解决吗?
我定义了我的REST方法,以返回String数据类型作为对http请求的响应.就是这个:
@Path("/users/{name}/")
@GET
@Produces("application/json")
public String getAllUserMemberships(@PathParam("name") String name) throws Exception{
String doc = "{\"name\":\""+name+"\",\"message\":\"Logged in\"}";
return doc;
}
Run Code Online (Sandbox Code Playgroud)
它工作正常,但有人告诉我宁愿返回一个javax.ws.rs.core.Response对象,如下面的示例代码所示.这也很好,他说这是响应HTTP请求的最佳方式但他不知道为什么.
@Path("/users/{name}/")
@GET
@Produces("application/json")
public Response getAllUserMemberships(@PathParam("name") String name) throws Exception{
String doc = "{\"name\":\""+name+"\",\"message\":\"Logged in\"}";
return Response.ok(doc, MediaType.APPLICATION_JSON).build();
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:当你可以返回一个String时,是否有必要将Response对象返回给HTTP请求.如果有必要,请告诉我为什么因为对于HTTP请求而言哪一个是正确的.我也担心Response对象可能会给我一些我可能无法处理的问题.
将 Android Studio 更新到版本 2.2.3 后,有时(多次)不显示日志
同样在重新运行应用程序后,它不显示日志
我尝试过logcat 中的重启按钮,但没有显示效果并检查了所有标准,例如No Filters, Verbose, selected emulator and application package.
重启 Android Studio 有效
更清楚的是,当我清除日志时,它不会再次显示日志,每次我必须重新启动 Android Studio 时
任何人都可以发布解决方案吗?
我正在尝试使用我的Android应用程序将一些数据存储在mysql数据库中。我正在使用okhttp3发送请求,但在此行出现一个错误:
client.newCall(request).execute();
Run Code Online (Sandbox Code Playgroud)
我在本地计算机和在线上尝试了它,但是它给了我同样的错误,这里是代码,
public class MainActivity extends AppCompatActivity {
String token1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("MainActivity is :" + FirebaseInstanceId.getInstance().getToken());
FirebaseMessaging.getInstance().subscribeToTopic("test");
FirebaseInstanceId.getInstance().getToken();
token1=FirebaseInstanceId.getInstance().getToken();
}
public void clicking(View view) {
OkHttpClient client = new OkHttpClient();
RequestBody body = new FormBody.Builder()
.add("Token",token1)
.build();
okhttp3.Request request = new okhttp3.Request.Builder()
.url("http://saleh923.byethost8.com/hii.html")
.post(body)
.build();
try {
client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是日志:
com.example.user.firebasenot E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.firebasenot, PID: 3280
java.lang.IllegalStateException: Could not execute method …Run Code Online (Sandbox Code Playgroud) 我是一名 JAVA 开发人员,但正在尝试转向 Kotlin,这表明我对 Kotlin 还很陌生。我有一个使用IDEAkotlin maven实现的项目。下面是pom 文件的构建部分。vertxintellij
我担心的是,当我使用IDEA构建项目时,jar文件没有生成到target文件夹中。我确实按照kotlinlang 页面的说明设置了 POM 文件,但问题仍然存在。
<build>
<plugins>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<goals> <goal>compile</goal> </goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/main/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/main/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<goals> <goal>test-compile</goal> </goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/test/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<executions>
<!-- Replacing default-compile as it is treated specially by maven -->
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<!-- Replacing default-testCompile as it is …Run Code Online (Sandbox Code Playgroud)