我想写从ConnectionRequest获取的响应数据到Codename中的本地Json文件,但我不知道从哪里开始......有人对此有任何线索,所以请张贴您的回答。ConnectionRequest响应处理
2
A
回答
0
我不确定你真正想做什么,但是对于处理JSON对象,你可能想尝试json.org提供的框架:https://mvnrepository.com/artifact/org.json/json 有了这个,你可以将响应转换成JSON对象然后从这个对象中提取数据。
这是一个HTTP-POST请求的示例:
public class PostTest {
public static void main(String[] args) {
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
//This section is for creating and executing the POST-Request
URIBuilder uriBuilder = new URIBuilder("http://www.example.com/");
URI uri = uriBuilder.build();
HttpPost request = new HttpPost(uri);
HttpResponse response = httpclient.execute(request);
//This section converts the response into a HttpEntity
HttpEntity entity = response.getEntity();
System.out.println(response.getStatusLine());
//This converts the HttpEntity into a JSON Object
if (entity != null) {
String responseString = EntityUtils.toString(entity);
JSONObject responseObject = responseString.getJSONObject();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
如果请求全成,你现在可以从responseObject
这样提取数据:
String fieldContent = responseObject.getString("fieldName");
System.out.println("Example field: " + fieldContent);
其中fieldName
表示在responseObject
中使用的变量的名称
我意识到这可能不是您正在寻找的确切场景因为,但我希望它可以给你一个想法。
+0
查看https://www.codenameone.com/ –
1
下面是使用ConnectionRequest.setDestinationFile()
将内容下载到本地文件的示例。
private static boolean downloadUrlTo(String url, String fileName, boolean showProgress, boolean background, boolean storage, ActionListener callback) {
ConnectionRequest cr = new ConnectionRequest();
cr.setPost(false);
cr.setFailSilently(true);
cr.setDuplicateSupported(true);
cr.setUrl(url);
if(callback != null) {
cr.addResponseListener(callback);
}
if(storage) {
cr.setDestinationStorage(fileName);
} else {
cr.setDestinationFile(fileName);
}
if(background) {
NetworkManager.getInstance().addToQueue(cr);
return true;
}
if(showProgress) {
InfiniteProgress ip = new InfiniteProgress();
Dialog d = ip.showInifiniteBlocking();
NetworkManager.getInstance().addToQueueAndWait(cr);
d.dispose();
} else {
NetworkManager.getInstance().addToQueueAndWait(cr);
}
return cr.getResponseCode() == 200;
}
1
下面的代码会从服务器获取一个JSON对象,并将其保存到本地文件。
String fspath = FileSystemStorage.getInstance().getAppHomePath();
String url = "http://example.com/getSomeJSON.php";
ConnectionRequest cr = new ConnectionRequest(url, false);
cr.setDestinationFile(fspath + "mylocalfile.json");
NetworkManager.getInstance().addToQueue(cr);
相关问题
- 1. 在ConnectionRequest中处理307和301
- 2. 响应处理
- 3. XML响应处理
- 4. $ .ajax()响应处理
- 5. 处理SAML响应
- 6. JSON响应处理
- 7. SOAP响应处理
- 8. Ajax响应处理
- 9. 处理SOAP响应
- 10. 处理响应wkWebKit
- 11. 处理Facebook的响应
- 12. Rails 3,处理ajax响应
- 13. 使用HttpResponseInterceptor处理响应
- 14. 帮助处理JSON响应
- 15. Android处理HTTP响应
- 16. SOLR - 处理查询响应
- 17. 处理GMAP API响应
- 18. 处理卷曲响应
- 19. Html,处理JSON响应
- 20. 用Javascript处理JSON响应
- 21. 利用Volley处理响应
- 22. 如何处理API响应
- 23. Jquery:处理json响应?
- 24. 处理NSURL请求响应
- 25. jQuery的处理JSON响应
- 26. 处理AJAX成功响应
- 27. 处理来自JSON响应
- 28. Android的HTTP响应处理
- 29. 在libcurl中处理响应
- 30. 的getJSON响应处理
'在Codename one'中 - 你是什么意思? – Divers
看看PropertyCross演示 – Diamond
@Divers https://www.codenameone.com/ –