PHP htmlentities json string to android textview text - java

I'm converting json string to display in android textview, I'm using following code in android
My json file look likes,
{"managementlist":[{"designation":"PRESIDENT","address":"<strong>Sri<\/strong><br>M\/s.Jewellery Mart<br>No:19\/1, Raghavaiah Road,<br>T.Nagar, Chennai - 600 017.","office":"+91 44 42122288","residence":"+91 44 28341155","centrix":"2159,#421155","fax":"+91 44 42122200","mobile":"+91 9940147199","email":"jayan1411#gmail.com","image":"http:\/\/mydomain.org\/images\/ssss.jpg?1419856154","divider":"0"},{"designation":"VICE PRESIDENT","address":"<strong>Sri Yogesh J Shah<\/strong><br>M\/s. Doimands<br>\r\n "Swarna sree", shop-201,<br># 36\/2,Veerappan Street,<br> Chennai -600 079.","office":"+91 44 25385336","residence":"+91 44 26442978","centrix":"#410024","fax":"+91 44 25387772","mobile":"+91 9382616888","email":"yogesh_shah31#yahoo.co.in","image":"http:\/\/mydomain.org\/images\/Sriyogesh.jpg?1419856154","divider":"0"}]}
And my java code looks like,
public class Managements {
private String designation;
private String address;
private String officePhone;
private String residencePhone;
private String centrixNo;
private String faxNo;
private String mobileNo;
private String email;
private String image;
private int header;
get() & set();
}
Json Parser:
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(inputStream));
for (String line = null; (line = bufferedReader
.readLine()) != null;) {
builder.append(line);
}
String returnStr = StringEscapeUtils
.unescapeJava(builder.toString());
String s = returnStr;
JSONObject json = new JSONObject(s);
JSONArray membersarray = json
.getJSONArray("managementlist");
for (int i = 0; i < membersarray.length(); i++) {
JSONObject member = membersarray.getJSONObject(i);
Log.i("JSON OBJECT :", member.getString("mobile")
+ " , " + member.getString("divider"));
String designation = member
.getString("designation");
String address = member.getString("address");
String officePhone = member.getString("office");
String residencePhone = member
.getString("residence");
String centrixNo = member.getString("centrix");
String faxNo = member.getString("fax");
String mobileNo = member.getString("mobile");
String email = member.getString("email");
String image = member.getString("image");
int header = member.getInt("divider");
list_managements.add(new Managements(designation,
address, officePhone, residencePhone,
centrixNo, faxNo, mobileNo, email, image,
header));
}
And in my adapter
addressView.setText(Html.fromHtml(managements.get(position).getAddress()),TextView.BufferType.SPANNABLE);
But my list view display the html string as like
<strong>Sri <\/strong><br>M\/s.Jewellery Mart<br>No:19\/1, Raghavaiah Road,<br>T.Nagar, Chennai - 600 017
This code didn't convert my html string to textview, What is my mistake. Please help anyone.

you need do script like this
// Textview variable
TextView bdeskripsi = (TextView) findViewById(R.id.ddeskripsi);
// Decode htmlentities
String htmlcodeenti = Html.fromHtml(yourdatajson).toString();
// Decode tag html to webview
bdeskripsi.setText(Html.fromHtml(htmlcodeenti));

Related

Unable to Parse string response

This is the response I tried to parse and try to get the value in the separate strings,
response =
"Data1{
key1='4722**********6',
key2='2107',
key3=value{
value1='226',
value2=passed,
value3=tracked,
value4=noted
},
isChecked=true
}"
but unable to parse the response, like
String Datas = "Data1{key1='4722**********6', key2='2107', key3=value{value1='226',
value2=passed,value3=tracked, value4=noted}, isChecked=true}";
String data = Datas.substring(5);
//String data1 = data.replaceAll("\"", "");
try {
JSONObject dataObject = new JSONObject((data)); //JsonObject
String strKey1= dataObject.getString("key1");
String strKey2= dataObject.getString("key2");
String strKey3= dataObject.getString("key3");
JSONObject strValueObj = new JSONObject(strKey3);
String strValue1= strValueObj.getString("value1");
String strValue2= strValueObj.getString("value2");
String strValue3= strValueObj.getString("value3");
String strValue4= strValueObj.getString("value4");
Will normal String parsing work or is it correct to parse using JSON? Can anyone please help me on parse the string response.
Well, you could do some data transformations to achive JSON format:
private static final String KEY3 = "key3";
private static final String VALUE = "value";
void extractData() throws JSONException {
String Datas = "Data1{key1='4722**********6', key2='2107', key3=value{value1='226', value2=passed,value3=tracked, value4=noted}, isChecked=true}";
String data = Datas.substring(5);
String dataAsJson = data.replaceAll("=", ":");
if (dataAsJson.indexOf(KEY3) > -1) {
StringBuilder dataBuilder = new StringBuilder(dataAsJson.replaceFirst(VALUE, "{" + VALUE + ":"));
dataBuilder.insert(dataAsJson.indexOf("}", dataAsJson.indexOf(KEY3)) + KEY3.length() - 1, "}");
dataAsJson = dataBuilder.toString();
}
JSONObject dataObject = new JSONObject((dataAsJson));
String strKey1= dataObject.getString("key1");
String strKey3= dataObject.getString("key3");
JSONObject key3Obj = new JSONObject(strKey3);
String value = key3Obj.getString("value");
JSONObject valueObj = new JSONObject(value);
String value1 = valueObj.getString("value1");
}
It works but for sure is not the most elegant solution and might not work if the input would change it's structure.
Edit: new input:
String Datas = "Data1{key1='4722**********6', key2='2107', key3=value{value1='226', value2=passed,value3=tracked, value4=noted}, isMasked=true}";
String data = Datas.substring(5);
String dataAsJson = data.replaceAll("=", ":");
if (dataAsJson.contains(KEY3)) {
StringBuilder dataBuilder = new StringBuilder(dataAsJson.replaceFirst(VALUE, "{" + VALUE + ":"));
dataBuilder.insert(dataAsJson.indexOf("}", dataAsJson.indexOf(KEY3)) + KEY3.length() - 1, "}");
dataAsJson = dataBuilder.toString();
}
JSONObject dataObject = new JSONObject((dataAsJson));
String strKey1 = dataObject.getString("key1");
String strKey3 = dataObject.getString("key3");
JSONObject key3Obj = new JSONObject(strKey3);
String value = key3Obj.getString("value");
JSONObject valueObj = new JSONObject(value);
String value1 = valueObj.getString("value1");

How to retrieve selected data from JSON Array?

The data on server I want to retrieve, data in JSON format but problem is that I want to retrieve only data having specific CHEF_NAME from JSON array.I am using Android Studio.
JSON Array
[{"status":"Success","recpie":[{"id":"162","image":"1580130013.png","cat_id":"17","name":"Tikka Boti Mix Recipe","chef_name":"Mehran","link":"https:\/\/www.youtube.com\/watch?v=sbPNGHIOpQY","uvlink":"","like":"2","view":"227"},{"id":"168","image":"1580211123.png","cat_id":"17","name":"Seekh Kabab Paratha Roll Recipe","chef_name":"Mehran","link":"https:\/\/www.youtube.com\/watch?v=q36Syxhjg3o","uvlink":"","like":"19","view":"158"},{"id":"176","image":"recipe_1583154151.jpg","cat_id":"17","name":"Windows","chef_name":"Imran Ahmed","link":"","uvlink":"","like":"2","view":"119"},{"id":"190","image":"recipe_1584214019.jpg","cat_id":"17","name":"Chicken Broast","chef_name":"Imran Developer","link":"","uvlink":"http:\/\/billing.synctechsol.com\/video\/VID-20200112-WA0020.mp4","like":"3","view":"108"},{"id":"161","image":"1580309239.png","cat_id":"14","name":"Sindhi Biryani Mix Recipe","chef_name":"Mehran","link":"https:\/\/www.youtube.com\/watch?v=OUG7gYTrnEk","uvlink":"","like":"4","view":"104"},{"id":"182","image":"recipe_1584126527.jpeg","cat_id":"17","name":"Broast","chef_name":"Imran Developer","link":"","uvlink":"","like":"1","view":"64"},{"id":"198","image":"recipe_1584737360.jpeg","cat_id":"17","name":"Distribution Board","chef_name":"Imran Ahmed","link":"","uvlink":"http:\/\/billing.synctechsol.com\/video\/VID-20200219-WA0035.mp4","like":"7","view":"60"},{"id":"191","image":"recipe_1584260119.png","cat_id":"17","name":"ggbvn","chef_name":"Imran Developer","link":"","uvlink":"http:\/\/billing.synctechsol.com\/video\/VID-20200315-WA0001.mp4","like":"1","view":"58"},{"id":"163","image":"1580134470.png","cat_id":"15","name":"Achar Gosht Mix Recipe","chef_name":"Mehran","link":"https:\/\/www.youtube.com\/watch?v=5EQWOSRpzHg","uvlink":"","like":"8","view":"54"},{"id":"101","image":"1580210522.png","cat_id":"12","name":"Fish Fry Mix Recipe","chef_name":"Mehran","link":"https:\/\/www.youtube.com\/watch?v=GeSloGLoPMk","uvlink":"","like":"9","view":"50"},{"id":"178","image":"recipe_1583407824.jpg","cat_id":"17","name":"uhnbj","chef_name":"hi igh","link":"","uvlink":"","like":"0","view":"47"},{"id":"177","image":"recipe_1583234674.jpeg","cat_id":"17","name":"Kabuli Pulao","chef_name":"Imran Ahmed","link":"","uvlink":"","like":"0","view":"46"}]}]
Java Code
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObject = jsonArray.getJSONObject(0);
String status = jsonObject.getString("status");
if (status.equals("Success")) {
JSONArray recipe = jsonObject.getJSONArray("recpie");
Log.e("Lenght", String.valueOf(recipe.length()));
for (int i = 0; i < recipe.length(); i++) {
JSONObject jsonObject1 = recipe.getJSONObject(i);
SearchGetSet searchGetSet = new SearchGetSet();
String id = jsonObject1.getString("id");
String image = jsonObject1.getString("image");
String cat_id = jsonObject1.getString("cat_id");
String name = jsonObject1.getString("name");
String chef_name = jsonObject1.getString("chef_name");
String link = jsonObject1.getString("link");
String uvlink = jsonObject1.getString("uvlink");
String like = jsonObject1.getString("like");
String view = jsonObject1.getString("view");
searchGetSet.setId(id);
searchGetSet.setRec_name(name);
searchGetSet.setRec_image(image);
searchGetSet.setLikes(like);
searchGetSet.setCat_id(cat_id);
searchGetSet.setChef_name(chef_name);
searchGetSet.setLink(link);
searchGetSet.setUVlink(uvlink);
searchGetSet.setView(view);
searchList.add(searchGetSet);}}
Create and add your searchGetSet object only if the chef_name is matching the name you're searching.
My guess is (I don't know Java), you've to use a simple if-statement, while looping :
for (int i = 0; i < recipe.length(); i++) {
JSONObject jsonObject1 = recipe.getJSONObject(i);
SearchGetSet searchGetSet = null; // Do not create your searchGetSet() object yet.
String chef_name = jsonObject1.getString("chef_name");
// Check the chef's name is 'John'.
if (chef_name.equals('John')) {
String id = jsonObject1.getString("id");
String image = jsonObject1.getString("image");
String cat_id = jsonObject1.getString("cat_id");
String name = jsonObject1.getString("name");
// ...
searchGetSet = new SearchGetSet();
searchGetSet.setId(id);
searchGetSet.setRec_name(name);
searchGetSet.setRec_image(image);
// ...
// Let's add the result we have found.
searchList.add(searchGetSet);
}
}

android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 22

i am aware that there is a lot of threads about this exception however, i would like people to view my code and assist me in where i am specifically going wrong.
Here is my code for trying to store my users input to an sqlite database and then transfer the code and display it in a new activity. Can anyone tell me where i am going wrong and why this error keeps occuring? (error in title)
Cursor res = myDb.getAllData();
StringBuffer buffer = new StringBuffer();
String getid = res.getString(0);
String getlt = res.getString(1);
String getpt = res.getString(2);
String getqty = res.getString(3);
String getdur = res.getString(4);
String getst = res.getString(5);
String getet = res.getString(6);
if ( res != null && res.moveToFirst()) {
do {
buffer.append(getid);
buffer.append(getlt);
buffer.append(getpt);
buffer.append(getqty);
buffer.append(getdur);
buffer.append(getst);
buffer.append(getet);
} while (res.moveToNext());
}
Intent TransferData = new Intent(getBaseContext(), screen4.class);
TransferData.putExtra("ID", getid);
TransferData.putExtra("LineType", getlt);
TransferData.putExtra("PackageType", getpt);
TransferData.putExtra("Quantity", getqty);
TransferData.putExtra("Duration", getdur);
TransferData.putExtra("Starttime",getst);
TransferData.putExtra("endtime", getet);
startActivity(TransferData);
}
});
}
}
ATTEMPT 2 INSIDE DO BLOCK :
Cursor res = myDb.getAllData();
StringBuffer buffer = new StringBuffer();
if ( res != null && res.moveToNext()) {
do {
String getid = res.getString(0);
String getlt = res.getString(1);
String getpt = res.getString(2);
String getqty = res.getString(3);
String getdur = res.getString(4);
String getst = res.getString(5);
String getet = res.getString(6);
buffer.append(getid);
buffer.append(getlt);
buffer.append(getpt);
buffer.append(getqty);
buffer.append(getdur);
buffer.append(getst);
buffer.append(getet);
Intent TransferData = new Intent(getBaseContext(), screen4.class);
TransferData.putExtra("ID", getid);
TransferData.putExtra("LineType", getlt);
TransferData.putExtra("PackageType", getpt);
TransferData.putExtra("Quantity", getqty);
TransferData.putExtra("Duration", getdur);
TransferData.putExtra("Starttime",getst);
TransferData.putExtra("endtime", getet);
startActivity(TransferData);
} while (res.moveToNext());
What you are trying to do is a unclear.
In your code there is:
String getid = res.getString(0);
String getlt = res.getString(1);
//bla bla bla
I believe they should be inside the do block.
You have tried to access the column data without moving the Cursor to first position.
You must call Cursor#moveToFirst(); before reading any value from the Cursor.

How to retrieve more than 25 youtube comments using Java

YouTubeService service = new YouTubeService("MyApp");
String videoEntryUrl = "http://gdata.youtube.com/feeds/api/videos/nU9dinwMyHo";
VideoEntry videoEntry = service.getEntry(new URL(videoEntryUrl), VideoEntry.class);
String commentUrl = videoEntry.getComments().getFeedLink().getHref();
CommentFeed commentFeed = service.getFeed(new URL(commentUrl), CommentFeed.class);
for(CommentEntry comment : commentFeed.getEntries()) {
System.out.println(comment.getPlainTextContent());
Here are my codes to retrieve the youtube comments of a specific video.
I'm able to retrieve about 25 comments using this code, but how do I retrieve ALL the comments from the video?
I think it is something like:
// Get a video entry
String str = "http://gdata.youtube.com/feeds/api/videos/" + videoId;
YouTubeQuery youtubeQuery = new YouTubeQuery(new URL(str));
String videoEntryUrl = youtubeQuery.getUrl().toString();
VideoEntry videoEntry = service.getEntry(new URL(videoEntryUrl),VideoEntry.class);
// Get the comments url for this video
if (videoEntry.getComments() != null) {
String commentUrl = videoEntry.getComments().getFeedLink().getHref();
System.out.println(commentUrl);
// Get the comment feed; use a new query
YouTubeQuery youtubeQuery = new YouTubeQuery(new URL(commentUrl);
youtubeQuery.setMaxResults(50);
youtubeQuery.setStartIndex(1);
String commentUrlFeed = youtubeQuery.getUrl().toString();
CommentFeed commentFeed = service.getFeed(new URL(commentUrlFeed),CommentFeed.class);
// The response should contain an url for the next feed, if any, already with an updated start-index.
for (int i = 0; i < commentFeed.getEntries().size()
&& commentFeed.getEntries().get(i) != null; i++) {
String author=commentFeed.getEntries().get(i).getAuthors().get(0).getUri().substring(41)
String commentId=commentFeed.getEntries().get(i).getId().substring(47);
String comment=commentFeed.getEntries().get(i).getPlainTextContent();
}
}
// Loop thru next comment feed call, if more can be expected.
// Use updated url from the response or set start-index = start-index + max-results.
}

Eclipse Syntax Error on Token with nothing apparently wrong

I've been trying to get this bit of Android code working for a while, and eclipse is giving me the ever so helpful Syntax Error when there shouldn't be one. I checked my brackets and I have the right number, and I believe they are all where they should be. This is code copied and pasted from a tutorial, and slightly tweaked. The code provided, in its entirety, works, but my version of it gives the errors. I didn't change that much.
public class MainActivity extends Activity{
Button Pull_Data;
// Define the TextViews
TextView client_Address1;
TextView client_Address2;
TextView client_Address3;
TextView client_Id;
// XML node keys
static final String KEY_ITEM = "Address"; // parent node
static final String KEY_PERSON = "addedByPerson";
static final String KEY_CITY = "addressCity";
static final String KEY_LINE_ONE = "addressLineOne";
static final String KEY_LINE_TWO = "addressLineTwo";
static final String KEY_STATE = "addressState";
static final String KEY_TYPE_ID = "addressTypeId";
static final String KEY_ZIP = "addressZip";
static final String KEY_CLIENT_ID = "clientId";
static final String KEY_COUNTRY_CODE = "countryCode";
static final String KEY_OBJECT_ID = "objectId";
static final String KEY_RECORD_ADDED_DATE = "recordAddedDate";
static final String KEY_RECORD_UPDATED_DATE = "recordUpdatedDate";
static final String KEY_RECORD_UPDATED_PERSON = "recordUpdatedPerson";
static final String KEY_SYNC_STATUS = "syncStatus"; //Syntax error is flagged here
// XML Data to Retrieve
Address = "";
addedByPerson = "";
addressCity = "";
addressLineOne = "";
addressLineTwo = "";
addressState = "";
addressTypeId = "";
addressZip = "";
clientId = "";
countryCode = "";
objectId = "";
recordAddedDate = "";
recordUpdatedDate = "";
recordUpdatedPerson = "";
syncStatus = "";
// Holds values pulled from the XML document using XmlPullParser
String[][] xmlPullParserArray = {{"Address", "0"},{"addedByPerson", "0"},{"addressCity", "0"},{"addressLineOne", "0"},{"addressLineTwo", "0"},
{"addressState", "0"},{"addressTypeId", "0"},{"addressZip", "0"},{"clientId", "0"},
{"countryCode", "0"},{"objectId", "0"},{"recordAddedDate", "0"},{"recordUpdatedDate", "0"},
{"recordUpdatedPerson", "0"},{"syncStatus", "0"}};
int parserArrayIncrement = 0;
// END OF NEW STUFF
#Override
protected void onCreate(Bundle savedInstanceState){ //Another error here, tagged at the first paren after onCreate
super.onCreate(savedInstanceState);
I have no clue what could be wrong. Structurally the code hasn't changed. The first error by static final String KEY_SYNC_STATUS = "syncStatus"; is a Synatx Error on token ";", { expected after this token
The two errors on the OnCreate method are Syntax Error on token "(" expected ; and Syntax Error on token ")" expected ; Any help is appreciated
Your error is here:
// XML Data to Retrieve
Address = "";
addedByPerson = "";
addressCity = "";
Your missing the types. What is Address, a String? What is addedByPerson?
// XML Data to Retrieve
String Address = "";
String addedByPerson = "";
String addressCity = "";
...
What is this? It is not valid parts of Java class:
//XML Data to Retrieve
Address = "";
Eclipse has it's own compiler that tries to compile file by parts. It is called incremental compiler.
Maybe it just can not split your class to compilable blocks? Try to solve these problems.

Categories