Deepu T
Deepu T

Reputation: 724

How to get the values from webservice in android

I wrote a web service in asp.net and called it in my android application using a JSON webservice. This is my code to call the web service from android

httpPost request = new HttpPost(url);
request.setHeader("Content-Type","application/json");
request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
HttpResponse response = client.execute(request);
String jsonResponse = EntityUtils.toString(response.getEntity());
JSONObject jobj=new JSONObject(jsonResponse);
JSONArray jsonArray=jobj.getJSONArray("d");
result=(String) jobj.getString("d")

I need to get each value from the result. How is it possible? My web service result is: {"d":"{\"Password\":33,\"Userid\":\"343\",\"Username\":fgfg}"}

Upvotes: 2

Views: 1104

Answers (2)

Hariharan
Hariharan

Reputation: 24853

Just remove below line

JSONArray jsonArray=jobj.getJSONArray("d");

because you don't ve any JSONArray in your response then d is the only object then remaining all values are in string because all in double cotes("")...

First of all,form your json like below:

{"d":{\"Password\":33,\"Userid\":\"343\",\"Username\":fgfg}}

So,that you can get,

JSONObject jobj=new JSONObject(jsonResponse);
JSONObject jsonObj=jobj.getJSONObject("d");

now you can get the value for Username like:

String Username = jsonObj.getString("Username");

Similarly you can get the other values also..

Upvotes: 1

LondonAppDev
LondonAppDev

Reputation: 9673

Just do

resultPassword = jsonArray.getString("Password");

That should work. Obviously change "Password" for each ID of the data you want to get.

Upvotes: 0

Related Questions