Java 连接请求响应处理
我正在尝试将从ConnectionRequest获取的响应数据写入本地Json文件,代码名为one,但我不知道从哪里开始。。。任何人对此有任何线索,请张贴您的答案 我不确定您实际想要做什么,但对于处理JSON对象,您可能希望尝试JSON.org提供的框架: 有了它,您可以将响应转换为JSON对象,然后从该对象提取数据 这是HTTP POST请求的一个示例:Java 连接请求响应处理,java,json,codenameone,Java,Json,Codenameone,我正在尝试将从ConnectionRequest获取的响应数据写入本地Json文件,代码名为one,但我不知道从哪里开始。。。任何人对此有任何线索,请张贴您的答案 我不确定您实际想要做什么,但对于处理JSON对象,您可能希望尝试JSON.org提供的框架: 有了它,您可以将响应转换为JSON对象,然后从该对象提取数据 这是HTTP POST请求的一个示例: public class PostTest { public static void main(String
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
我意识到这可能不是您想要的场景,但我希望它能给您一个想法。下面是一个使用
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;
}
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);
以下代码将从服务器获取JSON对象并将其保存到本地文件
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;
}
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);
在Codename one中
-你是什么意思?看看Property Crossdemo@Divers