I'm new in android ... so I need help. I have JSON link where I have for example like this
[{"id":"82","percent":"3","image_name":"something.jpg"....
I try some tutorial just for reading text, it was okay. But I have problem how to show in my layout the images from JSON...
Can you help or share some code about this?
Parse the json string : -
String jsonString = "your json string goes here" ;
JSONObject rootObj = new JSONObject(jsonString);
String link = rootObj.getString("image_name");
You can now use this url/link to convert it into bitmap : -
Bitmap image ;
InputStream in = new java.net.URL(link).openStream();
image = BitmapFactory.decodeStream(in);
in.close();
By default you can't disaplay image using web link. However lots of libraries have been created to solve this problem. You can try this one. There's also a list of alternative libraries.
Related
I know there's lots of Questions about this, and lots of answers too, however I haven't been able to find a solution to this problem yet, and wondered if anyone had some ideas.
Please note, I'm new to Android and have unfortunately inherited this project from an Android developer that's no longer with us.
Here's what I'm dealing with.
I have an Android App, that calls a web service, and gets a response Stream.
I've tried to use
String ScanString = new Scanner(inStream).useDelimiter("\\A").next();
which seems to work and results in a string as follows
{"RegResponseResult":"{\u000d\u000a \"REGISTERED\": true,\u000d\u000a \"COMPANY_URL\": \"http:\/\/111.222.3.444\/ABC\/XYZ.svc\/\"\u000d\u000a}"}
My Problem is .....
When I try to use JSONObject JSONObj = new JSONObject(ScanString) To Convert the string to a JSONObject, The Resutling Object Only has one nameValuePair.
ie: key=RegResponseResult and value={ "REGISTERED": true, \r\n"COMPANY_URL": "http://111.222.3.444/ABC/XYZ.svc/"\r\n}
}"
How can a jet a JsonObject, ignoring the 'RegResponseResult' , and using the Containing Tags instead, so that it ends up looking like this.
ie: JSONObj =
Key=REGESTERD value=true
Key=COMPANY_URL value=http://111.222.3.444/ABC/XYZ.svc
Thanks so much in advance.
The "RegResponseResult" is string so you may have to re-parse it again to get the inner json object.
String innerJson = JSONObj.getString("RegResponseResult");
JSONObject inner = new JSONObject(innerJson);
Thanks for the ideas.
I ended up changing my webservice, to output a stream instead of a string.
Turns out WCF Adds a bunch of \ to the string, which invalidates it as a Json String.
But returning it as a stream, doesn't result in anything being added.
Here's the Stream builder code for the webservice... if anyone else needs it.
//When Reading the DB in the Iaaa.csv
public Stream RegResponse(String DeviceData)
{
// *****connect to db and call stored proc goes here****
// .................
// read parameters back from cmd object.
DeviceRegisterResponse Response = new DeviceRegisterResponse();
Response.REGISTERED = Convert.ToBoolean(cmd.Parameters["REGISTERED"].Value);
Response.COMPANY_URL = Convert.ToString(cmd.Parameters["COMPANY_URL"].Value);
return GenerateStreamFromString(JsonConvert.SerializeObject(Response, Formatting.None));
}
//When passsing it back from the service in the Iaaa.cs
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "REG/{id}")]
Stream RegResponse(string id);
i have byteArray.
is it possible to convert byteArray to String?
please check my code
byte[] data = **some_byte_array**
JSONObject jsonObject = new JSONObject(data);
how do i fix this.
Try this
String decoded = new String(bytes, "UTF-8");
There are a bunch of encodings you can use, look at the Charset class in the Sun javadocs.
The "proper conversion" between byte[] and String is to explicitly state the encoding you want to use. If you start with a byte[] and it does not in fact contain text data, there is no "proper conversion". Strings are for text, byte[] is for binary data, and the only really sensible thing to do is to avoid converting between them unless you absolutely have to.
answer credit goes to https://stackoverflow.com/a/1536365/4211264
Yes you can convert byte array to String using one of the String constructors like this :
String myString = new String(yourByteArray);
Documentation for the same:
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#String(byte[])
All the best :)
This might be a simple question, so sorry for that. I'm trying to develop a radio app for my internet radio station. I'm using JSON format for the requests (Example JSON) to get information from my station. For example the "title" tag. I want to get this information.
My first try:
JSONObject data = response.getJSONObject("data");
songname.setText(data.getString("title"));
But the problem is that I cannot get this information. What can I do? Thanks in advance.
As your JSON do like this
JSONObject josnOBJ = new JSONObject(response);
JSONArray jArray = josnOBJ.getJSONArray("data");
JSONObject jsonData = null;
String title = null;
for (int i=0; i < jArray.length(); i++)
{
try {
jsonData = jArray.getJSONObject(i);
title= jsonData .optString("title");
} catch (JSONException e) {
// Oops
}
}
songname.setText(title);
You could use some generic JSON parsers instead of manual work. Eg: https://github.com/bajicdusko/AndroidJsonProvider
Just create model class regarding your json content. Check examples in README on github url.
Hope i could help.
You can use GSON library in case you have lot of values to fetch from JSON ,that would take care of everything for you.
I have to send an encrypted password with DES to a WebServer using a JSONObject encapsulated in a POST request. The problem is that, when I do this:
JSONObject jsonLogin = new JSONObject();
try{
jsonLogin.put("username", usernameS);
jsonLogin.put("password", passwordEncrypted);
}catch (JSONException e){
e.printStackTrace();
}
If I print the content of the JSON object with:
System.out.println("JSON to Server = "+jsonLogin);
the result is:
JSON to Server = {"password":"Qxw\/h16PVdE=\n","username":"XXXXXXXX#gmail.com"}
but the correct password is Qxw/h16PVdE=,so the Server doesn't recognize it.
I found some suggestion that indicate to use: string.replaceAll("\/","/");
But I would like to implement a clean solution.
Please give me any suggestions.
I guess you are doing something wrong while encrypting the data. Any way you can use this piece of code to manipulate the string after encryption.
String encryptedPassword = (String) jsonLogin.get("password");
if (!TextUtils.isEmpty(encryptedPassword) && encryptedPassword.endsWith("\n")) {
encryptedPassword = encryptedPassword.substring(0, encryptedPassword.length() - 1);
jsonLogin.put("password", encryptedPassword);
}
According to this answer
How to remove the \n, \t and spaces between the strings in java?
You can call passwordEncrypted.replaceAll("\\n+", ""); before writing value.
I need to decode JSONObject in Android 2.1 with Base64.I know that Base64 class supports Android 2.2+,that's why I include the source code in my project.So I need to do something like that :
JSONObject clientHash = new JSONObject();
byte[] tmpSecData = Base64.decode(clientHash.getJSONObject("client_auth_hash"));
Any suggestions how to do that or is it possible?
Lets try it,
Convert the clientHash.getJSONObject("client_auth_hash") in String then byteArray,
then use,
byte temp[];
Base64 b = new Base64();
String jsonString = clientHash.getJSONObject("client_auth_hash").toString();
temp = b.decode(jsonString.getBytes());
then use your temp byte[].
Hope this will help you. If its work then inform me. Thanx.