I'm using json parsing in my application first one has following http://shdg.com/webservice/interestreceiveddetail?version=apps&received_detail_id=2783&user_login_id=2650
Here I've so many fields from which I need to use two fields when user click on button
first one is user_status and another one is interest_id.
To send this request I've this URL
http:/fjshdjhs.com/webservice/interestreceivedprocess?version=apps&user_status=N&interest_id=1288
Now i need to send N with user_status,so how to do this..
Please try below code:
String endPoint="http://gujjumatch.com/webservice/interestreceivedprocess?";
String url;
try {
JSONObject resultObject = new JSONObject(response);
String user_status=resultObject.getString("user_status");
int interest_id=resultObject.getInt("interest_id");
if (user_status.equals("Decline")){
user_status="N";
}
String params="version=apps"
+"&user_status="+user_status
+"&interest_id="+interest_id;
url=endPoint+params;
Log.v("URL",url);
} catch (JSONException e) {
e.printStackTrace();
}
Related
How to parse more then one JSON which each ending with null character(through socket TCP/IP).
{"ObjectID":"UHJvY1dpcmVsZXNzTXNn","DeviceCode":"RUNEOjI=","ActiveInputNames":"Q2hlY2sgaW4gRmFpbA==","DeviceInputNo":"999999","Activation":false,"Reset":true,"LocationID":"","LocationGroupText":"","ProtocolText":"","CallBackNo":"OTE5MTgyNTcyMjQ5"}��{"ObjectID":"VFBpbmdPYmplY3Q="}��
As you can see the above response which has 2 JSON's each ending with null character...I can easily parse the single JSON but unable to parse more then one JSON..
It would be great if any one suggest any solutions!!
You can split the json string using the �� and loop through the array:
String s = "{\"ObjectID\":\"UHJvY1dpcmVsZXNzTXNn\",\"DeviceCode\":\"RUNEOjI=\",\"ActiveInputNames\":\"Q2hlY2sgaW4gRmFpbA==\",\"DeviceInputNo\":\"999999\",\"Activation\":false,\"Reset\":true,\"LocationID\":\"\",\"LocationGroupText\":\"\",\"ProtocolText\":\"\",\"CallBackNo\":\"OTE5MTgyNTcyMjQ5\"}��{\"ObjectID\":\"VFBpbmdPYmplY3Q=\"}��";
String[] array = s.split("��");
for (String string: array){
try {
JSONObject json = new JSONObject(string);
//do what ever you want with this
} catch (JSONException e) {
Log.e("Error",Log.getStackTraceString(e));
}
}
I made a chat app using socket.io and node.js and I got data like as in JSON object.
Here is my code
db.query('select email from users ', function(err, rows , field) {
if (err) throw err;
let getuser={"get":rows};
socket.emit('getuser',getuser);
});
In android I get the data and show the email data using Toast.makeText
JSONObject data = (JSONObject) args[0];
try {
String get =data.getString("get");
Toast.makeText(getApplicationContext(), get, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {}
But I wanna show email list (userlist) by using a simple ListView.
How can I get a string array from a JSON object?
Please help me.
You need to get JSON Array from JSONObject then convert it to List of model class by loop and parse it or by GSON Library then show result list on ListView
JSONObject data = (JSONObject) args[0];
try {
JSONArray userlist = data.getJSONArray("get");
} catch (JSONException e) {}
Am I missing something, I am trying to create a loginActivity and I created this code, and I have a log that logs the answer to the volley I created...But The if statement is not getting entered...Can someone see something I'm doing wrong? Sorry if its really dump( and yes I tried to change the response.equals check to be response.equals(" Password Accepted!");, this did not work either)
Another thing to note, yes I know my search in the log is different then the tag, it doesnt login, (which is what i want it to do)
Toast.makeText(getApplication(), response, Toast.LENGTH_LONG).show();
Log.e("Before Conversion",response);
String convertedResponse = null;
try {
convertedResponse = new String(response.getBytes("ISO-8859-1"), "UTF-8");
L.e("After Conversion",convertedResponse);
if (convertedResponse.equals("Password Accepted!")) {
showProgress(false);
Message msg = new Message();
msg.what = 1;
msg.obj = succcess ? 1 : 0;
handler.sendMessage(msg);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
06-29 15:24:04.990 16707-16707/E/Before Conversion: Password Accepted!
06-29 15:24:04.990 16707-16707/E/After Conversion: Password Accepted!
After suggestion to change logging...
06-29 15:34:32.339 16707-16707/ E/Got a response:: Password Accepted!
UPDATE
After creating a JSON array of length 1 with just the response string and a key for it, and passing it to my android studio code, the json array wont parse through the functions used to parse it (JSON Object, JSON Array), but manually parsing the response string works..... Less graceful then I would like it but i guess it works....
This can happen when encoding of response is not UTF-8. Try converting response like this:
String convertedResponse = null;
try {
convertedResponse = new String(response.getBytes("ISO-8859-1"), "UTF-8");
// do your things ...
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
your response is in JSONObject not JSONArray, so on Response Listnere use:
if(response.getString("response").equals("PASSWORD ACCEPTED!"){
//do what you want to do
}
Android 2.3.3
I am integrating Twitter API into my android application. I am using Twitter4j.
I could get the tweets for a particular user, using the below code.
Paging paging = new Paging(1, 40);
List<twitter4j.Status> statuses = twitter.getUserTimeline("xxx", paging);
Log.d(Const.TAG,"Showing Timeline.");
for (twitter4j.Status status : statuses) {
String rawJSON = DataObjectFactory.getRawJSON(status);
}
My requirement is that I need to parse the JSON String (rawJSON), to get some other values like, published date and so on.
I am pretty new to JSON parsing esp., with converting things to JSON. All i know in the above code is that, String rawJSON = DataObjectFactory.getRawJSON(status); converts the Status Object to a String and I can view it by logging it.
How do I convert this String to a JSONObject or a JSONArray? Can someone provide me with a sample code?
I tried the below code, but it gives an exception
try {
JSONObject tweet = new JSONObject(rawJSON);
JSONArray textArray = tweet.getJSONArray("text");
String text = textArray.getString(0);
Log.d(Const.TAG, "Text = "+text);
} catch (JSONException e) {
// TODO Auto-generated catch block
Log.d(Const.TAG, "Error while converting string to json");
e.printStackTrace();
}
Here's my code to parse a Twitter4j Status. This example get the language of the tweet.
//Status To JSON String
String statusJson = DataObjectFactory.getRawJSON(status);
//JSON String to JSONObject
JSONObject JSON_complete = new JSONObject(statusJson);
//We get another JSONObject
JSONObject JSON_user = JSON_complete.getJSONObject("user");
//We get a field in the second JSONObject
String languageTweet = JSON_user.getString("lang");
I got a way to get values from Status object without the need to convert it into JSON. Of course this is very basic, by I overlooked it.People who have trouble getting values from Status object, can look at this.
You can get all the values by using the object of Status.
For example, if "status" is an object of Status, we can get the text of the tweet, by using, status.getText() and the user value by using status.user() and so on. Just work on the status object a bit and you will find all the values you need.
{
"status": "ERROR",
"msg_content": "Your old password was entered incorrectly. Please enter it again.",
"code": "400",
"msg_title": "Sorry. Error in processing your request"
}
if (str.startsWith("Bad Request"))
{
textview.setText(" ");
}
How to print inside the textview to display the msg_content using json
You need to parse json using JSON object.
JSONObject obj = new JSONObject(str);
Then find string from JSON object whatever you want.
if (obj.has("msg_content")) {
String value = obj.getString("msg_content");
textview.settext(value);
}
You will need to create a JSONObject and pass it the string as a parameter.
JSONObject obj = new JSONObject(str);
Then to find a key in the JSONObject just call, always check if the JSONObject has that key before trying to retrieve it.
if (obj.has("status"){
String status = obj.getString("status");
}
if (obj.has("msg_content"){
String content = obj.getString("msg_content");
textview.setText(content);
}
JsonReader is implemented in API 11. If you want to use it in GingerBread or below try this
Use following Code for extracting Information from JSON Objects:
try {
Iterator keys = jsonObject.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
if(key.equals("msg_content"))
textView.setText(jsonObject.getString(key));
}
} catch (JSONException e) {
e.printStackTrace();
}
Also, if u have JSON as String, u can populate an object using following code:
try {
jsonObject = new JSONObject(theJsonString);
} catch (JSONException e1) {
e1.printStackTrace();
}
I'm quite new to JSON but I would create a JsonReader and parse the JSON as per here