I'm getting some data from a JSON webservice and I'm using a JSONParser I found on the internet. It is working fine for a JSON Array but I also want it to work with the response from Facebook.
The response I get from my own webservice is this:
[{"ID":"1", "Name":"Tester", "Available":"YES"}]
Now Facebook returns the response like this:
{"ID":"1", "Name":"Tester", "Available":"YES"}
The JSONParser I'm using can work with my webservice but how can I adjust the Parser so it can work with the Facebook response? It's probably an Array vs Object thing but I don't know how to adjust the Parser so it can process both types.
This is the JSONParser I'm currently using:
public class JSONParser {
static String response = null;
public final static int GET = 1;
public final static int POST = 2;
public JSONParser() {
}
public String makeServiceCall(String url, int method) {
return this.makeServiceCall(url, method, null);
}
public String makeServiceCall(String url, int method,
List<NameValuePair> params) {
try {
// http client
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpEntity httpEntity = null;
HttpResponse httpResponse = null;
// Checking http request method type
if (method == POST) {
HttpPost httpPost = new HttpPost(url);
// adding post params
if (params != null) {
httpPost.setEntity(new UrlEncodedFormEntity(params));
}
httpResponse = httpClient.execute(httpPost);
} else if (method == GET) {
// appending params to url
if (params != null) {
String paramString = URLEncodedUtils
.format(params, "utf-8");
url += "?" + paramString;
}
HttpGet httpGet = new HttpGet(url);
httpResponse = httpClient.execute(httpGet);
}
httpEntity = httpResponse.getEntity();
response = EntityUtils.toString(httpEntity);
Log.i("TEST", response);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}
}
0 comments:
Post a Comment