i'm currently developing and android app that collects data from ASP.NET WebApi Service. The service works but i can't get the JSON object in android,DO i have to convert to JSON array or to JSON object, i have tried alot of answers out there but no success. Here is my server side code snippet:
// GET api/uconnectservice
public String Get()
{
var json = JsonConvert.SerializeObject(
sampledata.GetAllDummy()
);
return json;
//return sampledata.GetAll();
}
which gives :
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
[{"AccountType":"15516321","CustomerName":"1MACMONSAM NICOLAS","Currentbalance":"1100,000.00","AllTransactions":[{"ID":103,"DescriptionoOfTransaction":"I need money right now","Debit":"5001","Credit":"0","TransactionDate":"15/12/2016 22:35:03","ValueDate":"15/12/2016 22:35:03"},{"ID":105,"DescriptionoOfTransaction":"I need money right now","Debit":"5002","Credit":"0","TransactionDate":"16/12/2016 22:35:03","ValueDate":"16/12/2016 22:35:03"},{"ID":107,"DescriptionoOfTransaction":"I need money right now","Debit":"5003","Credit":"0","TransactionDate":"17/12/2016 22:35:03","ValueDate":"17/12/2016 22:35:03"}......MORE JSON.........{"ID":113,"DescriptionoOfTransaction":"I need money right now","Debit":"5006","Credit":"0","TransactionDate":"20/12/2016 22:35:03","ValueDate":"20/12/2016 22:35:03"},{"ID":115,"DescriptionoOfTransaction":"I need money right now","Debit":"5007","Credit":"0","TransactionDate":"21/12/2016 22:35:03","ValueDate":"21/12/2016 22:35:03"},{"ID":117,"DescriptionoOfTransaction":"I need money right now","Debit":"5008","Credit":"0","TransactionDate":"22/12/2016 22:35:03","ValueDate":"22/12/2016 22:35:03"},{"ID":119,"DescriptionoOfTransaction":"I need money right now","Debit":"5009","Credit":"0","TransactionDate":"23/12/2016 22:35:03","ValueDate":"23/12/2016 22:35:03"},{"ID":121,"DescriptionoOfTransaction":"I need money right now","Debit":"50010","Credit":"0","TransactionDate":"24/12/2016 22:35:03","ValueDate":"24/12/2016 22:35:03"}]}]
</string>
etc which is fine
On the android side where i have to consume this service here is my code:
String URL = "http://192.168.1.101/UnicsApplication/api/uconnectservice";
JSONObject jsonObject;
private final OkHttpClient client = new OkHttpClient();
public void run() throws Exception {
Request request = new Request.Builder().url(URL).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
try (ResponseBody responseBody = response.body()) {
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response);
Headers responseHeaders = response.headers();
for (int i = 0, size = responseHeaders.size(); i < size; i++) {
Log.d("Results", responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
//Log.d("Results", responseBody.string());
try {
//get JSON objetc first
jsonObject = new JSONObject(responseBody.string());
Log.d("JsonObject", jsonObject.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The e.printstackTrance method is called.Error message i keep getting from Logcat is as follows:
12-15 01:59:44.280: W/System.err(26192): org.json.JSONException: Value {"operations":[{"AccountType":"15516321","CustomerName":"1MACMONSAM NICOLAS","Currentbalance":"1100,000.00","AllTransactions":[{"ID":103,"DescriptionoOfTransaction":"I need money right now","Debit":"5001","Credit":"0","TransactionDate":"16/12/2016 01:59:45","ValueDate":"16/12/2016 01:59:45"},{"ID":105,"DescriptionoOfTransaction":"I need money right now","Debit":"5002","Credit":"0","TransactionDate":"17/12/2016 01:59:45","ValueDate":"17/12/2016 01:59:45"},{"ID":107,"DescriptionoOfTransaction":"I need money right now","Debit":"5003","Credit":"0","TransactionDate":"18/12/2016 01:59:45","ValueDate":"18/12/2016 01:59:45"},{"ID":109,"DescriptionoOfTransaction":"I need money right now","Debit":"5004","Credit":"0","TransactionDate":"19/12/2016 01:59:45","ValueDate":"19/12/2016 01:59:45"},{"ID":111,"DescriptionoOfTransaction":"I need money right now","Debit":"5005","Credit":"0","TransactionDate":"20/12/2016 01:59:45","ValueDate":"20/12/2016 01:59:45"},{"ID":113,"DescriptionoOfTransaction":"I need money right now","Debit":"5006","Credit":"0","TransactionDate":"21/12/2016 01:59:45","ValueDate":"21/12/2016 01:59:45"},{"ID":115,"DescriptionoOfTransaction":"I need money right now","Debit":"5007","Credit":"0","TransactionDate":"22/12/2016 01:59:45","ValueDate":"22/12/2016 01:59:45"},{"ID":117,"DescriptionoOfTransaction":"I need money right now","Debit":"5008","Credit":"0","TransactionDate":"23/12/2016 01:59:45","ValueDate":"23/12/2016 01:59:45"},{"ID":119,"DescriptionoOfTransaction":"I need money right now","Debit":"5009","Credit":"0","TransactionDate":"24/12/2016 01:59:45","ValueDate":"24/12/2016 01:59:45"},{"ID":121,"DescriptionoOfTransaction":"I need money right now","Debit":"50010","Credit":"0","TransactionDate":"25/12/2016 01:59:45","ValueDate":"25/12/2016 01:59:45"},{"ID":103,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15001","Credit":"0","TransactionDate":"16/12/2016 01:59:45","ValueDate":"16/12/2016 01:59:45"},{"ID":105,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15002","Credit":"0","TransactionDate":"17/12/2016 01:59:45","ValueDate":"17/12/2016 01:59:45"},{"ID":107,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15003","Credit":"0","TransactionDate":"18/12/2016 01:59:45","ValueDate":"18/12/2016 01:59:45"},{"ID":109,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15004","Credit":"0","TransactionDate":"19/12/2016 01:59:45","ValueDate":"19/12/2016 01:59:45"},{"ID":111,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15005","Credit":"0","TransactionDate":"20/12/2016 01:59:45","ValueDate":"20/12/2016 01:59:45"},{"ID":113,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15006","Credit":"0","TransactionDate":"21/12/2016 01:59:45","ValueDate":"21/12/2016 01:59:45"},{"ID":115,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15007","Credit":"0","TransactionDate":"22/12/2016 01:59:45","ValueDate":"22/12/2016 01:59:45"},{"ID":117,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15008","Credit":"0","TransactionDate":"23/12/2016 01:59:45","ValueDate":"23/12/2016 01:59:45"},{"ID":119,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"15009","Credit":"0","TransactionDate":"24/12/2016 01:59:45","ValueDate":"24/12/2016 01:59:45"},{"ID":121,"DescriptionoOfTransaction":"I'am putting in money right now","Debit":"150010","Credit":"0","TransactionDate":"25/12/2016 01:59:45","ValueDate":"25/12/2016 01:59:45"},{"ID":103,"DescriptionoOfTransaction":"I need money right now","Debit":"5001","Credit":"0","TransactionDate":"16/12/2016 01:59:45","ValueDate":"16/12/2016 01:59:45"},{"ID":105,"DescriptionoOfTransaction":"I need money right now","Debit":"5002","Credit":"0","TransactionDate":"17/12/2016 01:59:45","ValueDate":"17/12/2016 01:59:45"},{"ID":107,"DescriptionoOfTransaction":"I need money right now","Debit":"5003","Credit":"0","TransactionDate":"18/12/2016 01:59:45","ValueDate":"18/12/20
12-15 01:59:44.280: W/System.err(26192): at org.json.JSON.typeMismatch(JSON.java:111)
12-15 01:59:44.280: W/System.err(26192): at org.json.JSONObject.<init>(JSONObject.java:159)
12-15 01:59:44.280: W/System.err(26192): at org.json.JSONObject.<init>(JSONObject.java:172)
12-15 01:59:44.280: W/System.err(26192): at com.nickSoft.Connections.OkHttpHandlerIncoming$1.onResponse(OkHttpHandlerIncoming.java:67)
12-15 01:59:44.280: W/System.err(26192): at okhttp3.RealCall$AsyncCall.execute(RealCall.java:126)
12-15 01:59:44.280: W/System.err(26192): at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
12-15 01:59:44.280: W/System.err(26192): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-15 01:59:44.280: W/System.err(26192): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-15 01:59:44.280: W/System.err(26192): at java.lang.Thread.run(Thread.java:841)
Is this a JSON string? what can i do to get JSON object which will be later converted to a collection?.Please any guidance or help will greatly appreciated. Thnx.
The <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
is not valid JSON, looks like you got first some xml that contains json. My suggestion is to strip it down correctly on the server side. If that is not possible remove the string tags from the response before you start parsing
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.