sorry for the stupid question but my knowledge of java net is terrible.
BAsically in my android application a call many php scripts to get data from a mysql db.
These data are returned in json format and i use Google json library to parse them.
Everything works fine but know in each php page i have to add a test. It the test is successfull, then the script continues and returns the json file, but if the test fails, the script return the string "false", or the value false (that's up to me) and my application instead of showing data has to redirect the user to a login page.
The code is the following:
URL url = new URL(Costanti.IP_SERVER+"myApps.php"+"?userId="+this.userId);
try
{
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
int status = conn.getResponseCode();
if (status >= 200 && status <= 299)
{
Reader r = new InputStreamReader(conn.getInputStream());
Applicazioni dati = new Applicazioni();
try
{
dati = gsonReader.fromJson(r, Applicazioni.class);
return dati;
}
catch (Exception e)
{
System.out.println("Ho fallito a prendere i dati");
Log.e("JSON_Parser",e.toString());
}
}
}
catch (IOException e)
{
System.out.println("Ho fallito la connnection");
e.printStackTrace();
}
}
So basically i use this google library to read the json file inside the imputStreamReader and fill the Applicazioni object with my data.
How can i check if the content of the imputStreamReader is the string "false" or the boolean false and if it's different parse it with the json library????
In the php at the end i do
echo json_encode($applicazione);
in one case or
echo "false" in the other case
Tnx
InputStream in = new URL(url).openStream();
Scanner scanner = new Scanner(new InputStreamReader(in));
String result = scanner.useDelimiter("\\Z").next(); // this reads the whole
// script output in a string
if(result.equals("false"))
handle the false value...
else
dati = gsonReader.fromJson(result, Applicazioni.class);
You can json encode the false result also like ["result"=>"false"] from PHP, This way you can always JSON decode in your Java program, and then look for result value.
You can put the result value in both cases in the output.
Related
Hello So I am writing an online translator using Yandex free tool .
I have this program that when user clicked on btn_translate I get the from and to languages from the comboBoxes then I get the text from the text label . Sending it to the server via the curl command.
keep in mind that i'm a beginner in this field so my code can have tons of problems ...
Here is the url sample:
https://translate.yandex.net/api/v1.5/tr.json/translate?key=API-KEY&lang=en-fa&text=hi
and here is the Json returned by the yandex:
{"code":200,"lang":"en-fa","text":["سلام"]}
So Here is my question:
I want to access the 3rd item in this Json as you can see it is "text" how should i do that ?
i am using Gson and i don't know how to put this Json i'm receiving in to an array which is like this :
Array[0] = 200 ( Code )
Array[1] = "en-fa" ( Lang )
Array[2] = "سلام" ( text )
and here is my code ( btn pushed part ) :
String command = IInfo.CMD +"&lang="+combo_from.getSelectedItem()
+"-"+combo_to.getSelectedItem()+"&text="+txt_word.getText();
System.out.println(command);
System.out.println("btn pushed");
try(Reader reader = new InputStreamReader(
Runtime.getRuntime().exec(command).getInputStream()
)){
JsonElement json = new JsonParser().parse(reader);
System.out.println(json);
} catch (IOException e)
{
e.printStackTrace();
}
and if you can explain to me how my try is working it would be awesome !
thanks.
With little changes to how you read the response you can do it like below:
JsonObject jsonObj = new JsonParser().parse(reader).getAsJsonObject()
jsonObj.get("code") ==> 200
jsonObj.get("lang") ==> "en-fa"
jsonObj.get("text").getAsString() ==> "سلام"
I have a response from a web server which contains the whole Base64 content of a pdf (I CANNOT CHANGE SERVER RESPONSE). Dimension of this file is over 50MB.
So, when i try to process this response with an AsynkTask the application runs into OOM exception.
#Override
protected String doInBackground(Object... params) {
String response = null;
try {
CommandCode = params[0].toString();
App application = (App) params[1];
String data = params[2].toString(); <--- Exception here
if (application != null) {
switch (CommandCode) {
//STORE ATTACHMENT
case Constants.ParserCommand.STORE_ATTACHMENT: {
try {
if (JSONParser.ParseGetAttachment(new JSONObject(data), application)) {
response = "attachment stored";
} else {
response = null;
}
} catch (Exception ex) {
response = null;
}
}
}
}
} catch (Exception ex) {
// some code
}
}
"Data" is a JSONObject which contains various informations including the base64 content. Above code is called by this few lines:
// GET_ATTACHMENT_BY_ATID
JSONObject jo = new JSONObject(_response).getJSONObject("DATA").getJSONObject("Attachment");
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, Constants.ParserCommand.STORE_ATTACHMENT, appCDA, jo);
All data are needed to create a new record into internal DB to store the document, but the content is stored into a File in filesystem.
My first thought is to isolate the content and work with it. Is there a way to do that? For example:
// All data withouth base64Content
JSONObject mainInfo = new JSONObject(_response).getJSONObject("DATA").getJSONObject("Attachment").without("Base64Content");
// Content isolated
String contentInfo = new JSONObject(_response).getJSONObject("DATA").getJSONObject("Attachment").getString("Base64Content");
Now the OOM exception still raise. Is there a way to split this base64 content in multiple parts and save them into the file above mentioned by appending?
Using s As FileStream = File.Open("C:\FilePath\File", FileMode.Open)
Using sr As New StreamReader(s)
Using reader As New JsonTextReader(sr)
While reader.Read()
//Perform your action here
End While
End Using
End Using
End Using
Here you can use FileStream to read the JSON file, it will only load apart of file in memory at a time and not the whole file. The code is in VB, you can implement it in Java in some way.
Hi I am using a spring mvc application to process excel and csv file. I have encountered one issue that for special characters such as DèéêàáâÉ once process it is converting it to D������� which is wrong.
However when the csv file encoding is UTF8 the special characters is converted successfully.
Part of the ajax call is shown below:
$('#fileuploading').fileupload({
url: 'uploadFile',
dataType: 'json',
acceptFileTypes: /(\.|\/)(csv|xlsx)$/i,
maxFileSize: 10000000,
autoUpload: false,
disableImageLoad: true,
disableAudioPreview: true,
disableVideoPreview: true,
disableValidation: false,
disableImageResize: true
})
My controller method is shown below:
#RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public #ResponseBody List<JSONResult> uploadFileHandler(
#RequestParam("files") MultipartFile file, HttpServletRequest request) {
logger.info("Starting upload of file: " + file.getOriginalFilename());
JSONResult result = null;
try {
result = uploadFile(file, appUserDTO, result, request);
} catch (IllegalStateException | IOException e) {
logger.error(e.getMessage() + e.getStackTrace());
errorLogService.saveErrorLog("FileUploadController: uploadFileHandler. Error: "+ e.getMessage(), appUserDTO.getUser().getUsrUid());
}
List<JSONResult> array = new ArrayList<>();
array.add(result);
return array;
}
Please find below method for processing the file
public CsvFileReader(String path, String delimeter, File file) throws FileNotFoundException {
String line="";
rows = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF8"))) {
while ((line = br.readLine()) != null) {
String[] lineData = line.split(delimeter,-1);
if(SanityCheck.isValid(lineData)){
rows.add(lineData);
}
}
} catch (IOException e) {
logger.error(e.getMessage());
}
}
Any one can point me out to the right direction how to solve this please?
Your program tries to read the files in UTF-8, therefore the files need to be in UTF-8 and it won't work if they aren't.
If you're asking how to handle files that can be in any encoding, the encoding of a file cannot be guessed, so you need to inform the server of the file's encoding when you upload it, using extra information such as a form field indicating the encoding.
If you're asking how to handle files that can be in any encoding, while you don't know how where to obtain from the encoding of a file because the files are just stashed there and you're not aware of any listing of the encoding of each of them, well like I said, it cannot be guessed.
If you feel like it, you can attempt to guess the encoding of the file, by reading it in UTF-8 first, and checking whether the result contains invalid characters. If not, reading it in UTF-8 was most likely correct. If there are invalid characters, then it's probable UTF-8 was not the correct encoding and you should try another. That other encoding may be windows-1252... And it may be something else entirely. No way to know, really.
I created a game and now I want to add a global highscore. I want to save the highscore on my server. I think the easiest way is to overwrite a textfile on my server that stores the scores and the names of the top players. How can I do this? The game is not running on my server! It is running on the client side.
Here is an example of writing a string to a file using the java.nio.file.Files class:
try {
String hightscore = "MyString";
Files.write(new File("D:/temp/file.txt").toPath(), hightscore.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
You can save it as a flat file like flavio.donze did or you can use a database.
So there is usually no relevance if you want to save it one the server or somewhere else. The path decides the location.
If you want to upload the scores from a client to a server, you can use multiple solutions.
F.e. adding per RMI or Webservice call
You can hire a simple hosting PHP/MySql and save the score in a database.
try {
URL url = new URL("http://exemple.com/saveScore.php");
InputStream is = url.openStream();
Scanner scanner = new Scanner(is);
scanner.useDelimiter("\\A");
String response = scanner.hasNext() ? scanner.next() : null;
if (response == "ok") {
System.out.println("Saved!");
}
}
catch (IOException e) {
e.printStackTrace();
}
In saveScore.php, after you save, just print ok.
<?php
// DB OPERATIONS
echo "ok";
exit;
I'm try to develop a Java application with GSON library to parse JSON (from PHP file (encoding in UTF-8) -> json_encode)
My php source :
<?php
$base = mysql_connect ('****', '*****', '*****');
mysql_select_db ('*****', $base) ;
$req = mysql_query("SELECT ***, ****, ***, ****, **** from *****");
function jsonRemoveUnicodeSequences($struct) {
return preg_replace("/\\\\u([a-f0-9]{4})/e", "iconv('UCS-4LE','UTF-8',pack('V', hexdec('U$1')))", json_encode($struct));
}
while ($row = mysql_fetch_array($req)) {
$output[] = $row;
}
print(jsonRemoveUnicodeSequences($output));
mysql_free_result ($req);
?>
JSON string is properly displayed with accent in in my web browser.
My Java source :
BufferedReader reader = null;
try {
URL url = new URL("**************");
URLConnection urlConnection = url.openConnection();
reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
System.out.println("JSON data issu du PHP : "+ line + "\n");
Gson gson = new Gson();
Type type = new TypeToken<List<AlertTable>>(){}.getType();
ArrayList<AlertTable> bddListJson = gson.fromJson(line, type);
bddList = (ArrayList<AlertTable>) bddListJson.clone();
}
} catch (IOException e) {
//
} finally {
if (reader != null) {
//
}
}
System.out.println(bddList.get(1).getTypeAlert());
System.out.println(bddList.get(1).getLigne());
System.out.println(bddList.get(1).getSens());
System.out.println(bddList.get(1).getStation());
System.out.println(bddList.get(1).getTimeAlert());
And in console, character with accent is replaced by "?".
Any idea ?
First, note that your code may fail on some systems because MySQL uses a connection charset to move text between the client and the server. You should issue a raw query like SET NAMES <charset> to set the encoding of the input data available in your script.
Now, assuming there's no transcoding issue (because your columns only store ASCII characters and all charset involved are ASCII-compatible), I wrote the following code to replace \u0000 escape sequences with UTF8-encoded strings:
<?php
while ($row = mysql_fetch_array($req)) {
$out[] = preg_replace_callback("/\\\\u([a-f0-9]{4})/i", "unescape", $row);
}
// On PHP 5.4+ use json_encode($out, JSON_UNESCAPED_UNICODE)
echo json_encode($out);
/* Accept the matcher array
* return the UTF-8 encoded string
*/
function unescape($match) {
return call_user_func_array('pack', get_pack_args(hexdec($match[1])));
}
function get_pack_args($cp) {
if ($cp < 0x80) return array('C1', $cp);
if ($cp < 0x0800) {
$length = 2;
} else if ($cp < 0x010000) {
$length = 3;
} else {
$length = 4;
}
$args[0] = "C{$length}";
// lead byte
$args[1] = (0xFE << (7 - $length)) | ($cp >> (6 * ($length - 1)));
// continuation bytes
for ($l = 0; $l < ($length - 1); $l++) {
$args[$length - $l] = 0x80 | (($cp >> (6 * $l)) & 0x3F);
}
// stupid PHP...
ksort($args);
return $args;
}
You should test the code deeply, however it's a good starting point. For displaying in a browser, you should specify the encoding with a HTTP header:
header('Content-Type: application/json; charset=utf8');
Note that json_encode() escapes unicode sequences on its part by default. This is inefficient, so you may want to use the JSON_UNESCAPED_UNICODE flag, or choose another JSON library for encoding.
At this point the server part should be ok, and you can test it with your browser. If it works, but the Java program can't still show the right characters on the console, it may be a problem with the console itself, or even with the font used by the console application. Without the actual data one can't tell, however follow my advice and print the JSON to a text file UTF8-encoded. Then open it with a text editor (specifying UTF-8 if needed) and tell what you see.
Finally, note that the usage of the mysql PHP extension is discouraged. Use mysqli or PDO instead.
if You set parameter of json_encode Correctly like
json_encode($WhoRank,JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE);
no problem in encoding value and transfer data but if is not work again you can Use urlencode for Encode UTF-8 Value and Decode it on Java
like this
urlencode('aیgfسبd');
output
a%DB%8Cgf%D8%B3%D8%A8d
with this output You haven't problem in json value.