I'm trying to call to my API sending a JSON to delete a product from my DB; however, it doesn't delete anything.
The response of the JSON is "true," and it doesn't give to me any error; even so, when I make a query on my DB, the product is still there.
I've created a class called HttpDeleteWithBody that looks like:
class HttpDeleteWithBody extends HttpEntityEnclosingRequestBase {
public static final String METHOD_NAME = "DELETE";
public String getMethod() { return METHOD_NAME; }
public HttpDeleteWithBody(final String uri) {
super();
setURI(URI.create(uri));
}
public HttpDeleteWithBody(final URI uri) {
super();
setURI(uri);
}
public HttpDeleteWithBody() { super(); }
}
And then on my doInBackGround of my Fragment, I do this:
boolean resul = true;
try {
JSONObject usuari = new JSONObject();
try {
usuari.put("idProducte", params[0]);
usuari.put("idusuari", params[1]);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpEntity entity = new StringEntity(usuari.toString());
HttpClient httpClient = new DefaultHttpClient();
HttpDeleteWithBody httpDeleteWithBody = new HttpDeleteWithBody(getResources().getString(R.string.IPAPI) + "produsuaris/produsuari");
httpDeleteWithBody.setEntity(entity);
HttpResponse response = httpClient.execute(httpDeleteWithBody);
Log.d("Response ---------->", response.getStatusLine().toString());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception ex) {
Log.e("ServicioRest", "Error!", ex);
}
return resul;
Furthermore, I've tried to do this:
HttpDeleteWithBody delete = new HttpDeleteWithBody(getResources().getString(R.string.IPAPI) + "produsuaris/produsuari");
StringEntity se = new StringEntity(usuari.toString(), HTTP.UTF_8);
se.setContentType("application/json");
delete.setEntity(se);
however, it doesn't work... the log says:
D/Response ---------->﹕ HTTP/1.1 200 OK
This is how I call the method:
JSONObject deleteproduct = new JSONObject();
try {
deleteproduct.put("idProducte", String.valueOf(IDPROD));
deleteproduct.put("idusuari", String.valueOf(IDUSU));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i("Json test per afegir prod --> ", deleteproduct.toString());
TareaWSInsertar tarea = new TareaWSInsertar();
tarea.execute(String.valueOf(IDPROD), String.valueOf(IDUSU));
I've added on my Google Chrome a plug-in called "PostMan" and when I try to do this by this way, it's deleting correctly...
What I'm doing wrong?
EDIT
I tried to use cURL, and this is the result:
It is returning me false, when I put the same JSON as PostMan; nevertheless, if I put the same JSON on PostMan, it works fine.
EDIT 2
I implemented ion library and I did it like :
JSONObject usuari = new JSONObject();
try {
usuari.put("idProducte", params[0]);
usuari.put("idusuari", params[1]);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
String url = getResources().getString(R.string.IPAPI) + "produsuaris/produsuari";
Log.d("CURL", "curl -X DELETE -d '" + usuari.toString() + "' " + url);
Builders.Any.F builder = Ion.with(getActivity().getApplicationContext())
.load(HttpDelete.METHOD_NAME, url)
.setTimeout(15000).setStringBody(usuari.toString());
String response = builder.toString();
Log.d("TEST", "Req response -->" + response);
}
catch (Exception ex){
resul = false;
}
And it still returning that it's OK, and don't delete anything.
This appears to be a server side issue, to be sure of this, do the following:
1) Add Ion as an dependency in your grandle.
compile 'com.koushikdutta.ion:ion:+'
2) Use the following snippen to perform your request:
JSONObject jsonObject = new JSONObject();
try{
for(BasicNameValuePair aNameValue : getParameters()){
jsonObject.put(aNameValue.getName(), aNameValue.getValue());
Log.d("TEST","parameter "+aNameValue.getName()+": "+aNameValue.getValue());
}
jsonObject.put("time_zone", Util.timeZone());
Log.d("TEST","parameter time_zone:"+Util.timeZone());
}catch(Exception e){
//
}
Log.d("CURL", "curl -X DELETE -d '"+jsonObject.toString()+"' "+getUrl());
Builders.Any.F builder = Ion.with(getContext())
.load(HttpDelete.METHOD_NAME, getUrl())
.setTimeout(BuildConfig.HttpClientMaxTimeout).setStringBody(jsonObject.toString());
String response = builder.asString().get();
Util.checkThreadUiException();
Log.d("TEST","-->"+ response);
There's no much rocket science, this is the code that I used in an app, in that method I received the parameters to send as a json, as a BasicNameValuePair collection. You can change that and directly set your json. I'm 100% porcent sure that this request will fail, because this is a server side issue.
UPDATE
JSONObject usuari = new JSONObject();
try {
usuari.put("idProducte", params[0]);
usuari.put("idusuari", params[1]);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String url = getResources().getString(R.string.IPAPI) + "produsuaris/produsuari";
Log.d("CURL", "curl -X DELETE -d '"+usuari.toString()+"' "+url);
Builders.Any.F builder = Ion.with(getContext())
.load(HttpDelete.METHOD_NAME, url)
.setTimeout(BuildConfig.HttpClientMaxTimeout).setStringBody(usuari.toString());
String response = builder.asString().get();
Log.d("TEST","Req response -->"+ response)
UPDATE
Try this, perform this request through curl and let me know the result:
curl --http1.0 -X DELETE -d '{"idusuari":121,"idProducte":15}' 192.168.1.46/ServicioWebRest/api/produsuaris/produsuari
Doing this you're telling to curl to send the request through http 1.0, chunked responses are only supported by http 1.1, if there's an error in the chunk encoding, this should tell you.
Also take a look to this issue that I submitted to Ion long ago. I think that the problem that I was having that time, and your current problem are alike, maybe some of the tips there will help. Specially the part about the addHeader("Connection", "close").
Would look like this:
Builders.Any.F builder = Ion.with(getContext())
.addHeader("Connection", "close")
.load(HttpDelete.METHOD_NAME, getUrl())
.setTimeout(BuildConfig.HttpClientMaxTimeout).setStringBody(jsonObject.toString());
Finally I solved the problem, what I've done is change the HttpDelete method on my API, and instead of send a JSON, I send parameters (like a HttpGet) and now my code is like :
boolean resul = true;
HttpClient httpClient = new DefaultHttpClient();
String id____USER = params[0];
String id____PROD = params[1];
HttpDelete del =
new HttpDelete(getResources().getString(R.string.IPAPI) + "produsuaris/produsuari?idProd=" + Integer.parseInt(id____PROD)+"&idUs="+Integer.parseInt(id____USER));
del.setHeader("content-type", "application/json");
try
{
HttpResponse resp = httpClient.execute(del);
String respStr = EntityUtils.toString(resp.getEntity());
if(!respStr.equals("true"))
resul = false;
}
catch(Exception ex)
{
Log.e("ServicioRest","Error!", ex);
resul = false;
}
return resul;
This is how I call this AsyncMethod
TareaWSInsertar tarea = new TareaWSInsertar();
tarea.execute(String.valueOf(IDUSU),String.valueOf(IDPROD));
This work to me, I know it's not the best solution, but I've no much time, also I tried three solutions and noone didn't work.
Feel free to post a correct answer if you know what I was doing wrong.
Related
I am working on an Android project and trying to get Digest Authentication to work with Retrofit. I'm kind of amazed Retrofit doesn't natively support it (or more accurately, that OkHttp doesn't support it), but no point complaining I suppose.
I cruised through quite a few threads here and it appears the right solution is to integrate the Apache HttpClient (which natively supports Digest Auth) with Retrofit. This requires wrapped the HttpClient with a retrofit.client.Client implementation. The retrofit incoming values have to be parsed and built into a new HttpClient response which is then sent back to Retrofit to be processed normally. Credit to Jason Tu and his example at: https://gist.github.com/nucleartide/24628083decb65a4562c
Issue is, it isn't working. I'm getting a 401 Unauthorized every time and it's not clear to me why. Here's my Client impl:
public class AuthClientRedirector implements Client {
private final CloseableHttpClient delegate;
public AuthClientRedirector(String user, String pass, String hostname, String scope) {
Credentials credentials = new UsernamePasswordCredentials(user, pass);
AuthScope authScope = new AuthScope(hostname, 443, scope);
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(authScope, credentials);
delegate = HttpClientBuilder.create()
.setDefaultCredentialsProvider(credentialsProvider)
.build();
}
#Override
public Response execute(Request request) {
//
// We're getting a Retrofit request, but we need to execute an Apache
// HttpUriRequest instead. Use the info in the Retrofit request to create
// an Apache HttpUriRequest.
//
String method = request.getMethod();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
if (request.getBody() != null) {
try {
request.getBody().writeTo(bos);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String body = new String(bos.toByteArray());
HttpUriRequest wrappedRequest;
switch (method) {
case "GET":
wrappedRequest = new HttpGet(request.getUrl());
break;
case "POST":
wrappedRequest = new HttpPost(request.getUrl());
wrappedRequest.addHeader("Content-Type", "application/xml");
try {
((HttpPost) wrappedRequest).setEntity(new StringEntity(body));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case "PUT":
wrappedRequest = new HttpPut(request.getUrl());
wrappedRequest.addHeader("Content-Type", "application/xml");
try {
((HttpPut) wrappedRequest).setEntity(new StringEntity(body));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case "DELETE":
wrappedRequest = new HttpDelete(request.getUrl());
break;
default:
throw new AssertionError("HTTP operation not supported.");
}
CloseableHttpResponse apacheResponse = null;
try {
apacheResponse = delegate.execute(wrappedRequest);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(apacheResponse!=null){
// Perform the HTTP request.
CloseableHttpResponse response = null;
try {
response = delegate.execute(wrappedRequest);
// Return a Retrofit response.
List<Header> retrofitHeaders = toRetrofitHeaders(
response.getAllHeaders());
TypedByteArray responseBody;
if (response.getEntity() != null) {
responseBody = new TypedByteArray("",
toByteArray(response.getEntity()));
} else {
responseBody = new TypedByteArray("",
new byte[0]);
}
System.out.println("this is the response");
System.out.println(new String(responseBody.getBytes()));
return new retrofit.client.Response(request.getUrl(),
response.getStatusLine().getStatusCode(),
response.getStatusLine().getReasonPhrase(), retrofitHeaders,
responseBody);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//failed to return a new retrofit Client
return null;
}
private List<Header> toRetrofitHeaders(org.apache.http.Header[] headers) {
List<Header> retrofitHeaders = new ArrayList<>();
for (org.apache.http.Header header : headers) {
retrofitHeaders.add(new Header(header.getName(), header.getValue()));
}
return retrofitHeaders;
}
private byte[] toByteArray(HttpEntity entity) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
entity.writeTo(bos);
return bos.toByteArray();
}
}
My retrofit configuration looks like this:
public final RestAdapter configureService(){
AuthClientRedirector digestAuthMgr = new AuthClientRedirector(username,password,"myhostname","public");
RestAdapter.Builder builder = new RestAdapter.Builder()
.setEndpoint("http://myhostname:8003/endpoint")
.setLogLevel(RestAdapter.LogLevel.FULL)
.setClient(digestAuthMgr);
return builder.build();
}
I am stumped why I'm consistently getting 401s back from the server. I've walked through the response building process and it looks clean to me, so I'm thinking I'm missing something fundamental. The credentials are good and I have verified them outside the app. Anyone walked this walk before?
You are using port number 443 for authentication.
AuthScope authScope = new AuthScope(hostname, 443, scope);
But, it seems that your real port number is 8003.
RestAdapter.Builder builder = new RestAdapter.Builder()
.setEndpoint("http://myhostname:8003/endpoint")
So, how about use port number 8003 for authentication like below?
AuthScope authScope = new AuthScope(hostname, 8003, scope);
When I run the following code, I get a "connection reset by peer" exception in most instances.
String rest = System.setProperty("http.keepAlive", "false");
String uri = WEB_LOG_IN + "?user_username=" + user_email
+ "&user_password=" + user_pass + "&user_mac=" + user_mac;
HttpClient client = new DefaultHttpClient();
HttpPost httppost = new HttpPost(uri);
HttpResponse response;
String s;
try {
response = client.execute(httppost);
s = new String(EntityUtils.toByteArray(response.getEntity()),
"UTF-8");
// read json
if (s.length() > 2)
return readlogin_info(s);
else
return false;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
String rest = System.setProperty("http.keepAlive", "false");
returns null which means no "http.keepAlive" property.
I tried using httpurlconnection, but that also didn't work.
I am so confused because I think the code works without issue for some ISPs.
Based on my investigation the exception caused by Mikrotik Router latency! (Maybe! Please correct me if I am wrong!)
so I put the execute section in a while until it can get the answer from server, after multiple execution I see after 2 times of trying to connect it successfully get the answer.
int count = 0;
while (s == null) {
count++;
try {
response = client.execute(httpGet);
s = new String(
EntityUtils.toByteArray(response.getEntity()),
"UTF-8");
// read json
if (s.length() > 2)
{
return readlogin_info(s);
}
else
{
return false;
}
} catch (Exception e) {
e.printStackTrace();
}
I am working on https post request.
I did successfully http post request but i don't know how to change it with https SSL crt.
How can I add SSL crt in project and how to convert http to https.
I tried many examples but i didn't get it.
My http post request code is.. saVersion is my lib
public class ServerCommunication implements Runnable, IServerCommunication {
private static final String TAG = ServerCommunication.class.getSimpleName();
private String url;
private String userAgent;
private byte[] data;
static
{
System.loadLibrary("saNative");
}
private static void receiveBytestream(byte[] stream)
{
saVersion.getInstance().onSecurePacketReceived(stream);
}
/**
* Functions as a container to create other (meaningfuller) instances only
*/
public ServerCommunication()
{
Log.d(TAG, "Note this class is deprecated");
}
private ServerCommunication(String _url, String _userAgent, byte[] _data)
{
url = _url;
userAgent = _userAgent;
data = _data;
}
public void run()
{
DefaultHttpClient httpclient = new DefaultHttpClient();
if(url.equals(""))
{
Log.e(TAG, "URL is an empty string... aborting sending procedure");
return;
}
// make URL
HttpPost httpost = new HttpPost(url);
StringEntity se;
try {
se = new StringEntity(new String(data) + "\r\n");
httpost.setEntity(se);
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-Type", "application/json");
// Get User Agent String
httpost.setHeader("User-Agent", userAgent); // set string
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpResponse response = httpclient.execute(httpost);
InputStreamReader sr = new InputStreamReader(response.getEntity().getContent());
byte[] respContent = IOUtils.toByteArray(sr);
receiveBytestream(respContent);
}
catch (ClientProtocolException e)
{
Log.e(TAG, "AS-Connection error: Probably Internet-Permission is not set in your manifest?");
e.printStackTrace();
}
catch (IOException e)
{
Log.e(TAG, "AS-Connection error: Probably Internet-Permission is not set in your manifest?");
e.printStackTrace();
}
finally
{
}
}
#Override
public void sendSecurePacket(String _url, byte[] _data, String userAgent) {
ServerCommunication sc = new ServerCommunication(_url, userAgent, _data);
Thread t = new Thread(sc);
t.start();
}
}
I faced this same issue before some days, I have published into my blog. Refer it. Hope it helps you regarding same.
I know I can link to the Play Store with the URL:
market://details?id=com.example.appname
What I would love to do is 'ping' this URL in the background, and determine if the app is actually available, then, I can modify my UI as appropriate.
Let's assume that, if app is unavailable, the page under http://play.google.com/store/apps/details?id=<package_name> contains, for example, word error-section. If app is available, it does not contain this word.
Make HTTP GET to that URL and search for error-section.
No error-section - your app is available.
Otherwise, it's unavailable.
Like this:
final HttpClient client = new DefaultHttpClient();
final String getURL = "http://play.google.com/store/apps/details?id=<package_name>";
final HttpGet get = new HttpGet(getURL);
final HttpResponse responseGet = client.execute(get);
final HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null)
{
final String response = EntityUtils.toString(resEntityGet);
if (response.indexOf("error-section") == -1)
{
// available
}
else
{
// unavailable
}
}
private class URLTest extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... params) {
try {
try {
HttpClient httpclient = new DefaultHttpClient();
String strurl = "https://play.google.com/store/apps/details?id=com.rovio.angrybirdsstarwars.ads.iap";
// String strurl =
// "https://play.google.com/store/apps/details?id=com.rovio.angrybirdsstarwars.ads.iaptest";
// //fake packagename append with "test"
HttpGet httpget = new HttpGet(strurl);
HttpResponse httpresp = httpclient.execute(httpget);
if (httpresp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
Log.e("Found", "YES");
} else if (httpresp.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
Log.e("Found", "NO");
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
Log.e("", "");
}
return null;
}
}
i tried with correct & fake packages and its giving bad request responze if the app not available & positive(success 200 code) if app is there..so plz check it once it might help u.
android 2.2 & app min req 2.3
I'm trying to connect to Google tasks without using Google client libraries. The following code returns a 403 forbidden error. Just not sure what I'm missing. Any guidance would be appreciated.
try {
Bundle options = new Bundle();
AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
Account[] list = manager.getAccountsByType("com.google");
Account acct = list[0];
manager.invalidateAuthToken("com.google", null);
AccountManagerFuture<Bundle> acc = manager.getAuthToken(
acct,
"oauth2:https://www.googleapis.com/auth/tasks",
options, true, null, null);
Bundle bundle = acc.getResult();
String token = bundle
.getString(AccountManager.KEY_AUTHTOKEN);
Log.i("Token: ", token); // token does have value
String url = "https://www.googleapis.com/tasks/v1/users/#me/lists?key=long_winded_api_key_from_console_here";
HttpGet getRequest = new HttpGet(url);
getRequest.addHeader("client_id",
"clientID_from_console_here.apps.googleusercontent.com");
getRequest.addHeader("Authorization", "OAuth " + token);
HttpClient httpclient = new DefaultHttpClient();
String responseBody = httpclient.execute(getRequest,
new BasicResponseHandler()); // exception raised here
httpclient.execute(getRequest, new BasicResponseHandler());
Log.i("###", responseBody); // cannot get the response here
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // exception raised here
catch (OperationCanceledException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AuthenticatorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
The following link shows how to get started using the Google Drive API from Android. It lets the user pick an account, gets consent from them then acquires a credentials object that can be used for API access with the Google client libraries:
https://developers.google.com/drive/quickstart-android
In your case you are trying to use the Tasks API, however the authentication parts should be identical:
In Step 2, enable the Tasks API instead.
Step 4 shows how to get an access token for a particular scope:
credential = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE);
startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
Change the scope to the task API scope: https://www.googleapis.com/auth/tasks
Then I would recommend using the Google client libraries for the rest like the Drive example.
If for any reason you don't want to use the client libraries, and prefer crafting the HTTP request yourself, the authorization header you want should look like the following (unable to test right now):
getRequest.addHeader("Authorization", "Bearer " + credential.getToken());
I'm not perfectly sure if this is the cause, since you seem to do the same steps
I do, but you might want to try to use "Manage your tasks" instead of
"oauth2:https://www.googleapis.com/auth/tasks". That might be the reason for
the 403.
Here is how I connect without client libraries.
Full source available here:
apiTalker
First I get the access token:
public static final String AUTH_TOKEN_TYPE = "Manage your tasks";
public static String getAuthToken(AccountManager accountManager,
Account account, String authTokenType, boolean notifyAuthFailure) {
Log.d(TAG, "getAuthToken");
String authToken = "";
try {
// Might be invalid in the cache
authToken = accountManager.blockingGetAuthToken(account,
authTokenType, notifyAuthFailure);
accountManager.invalidateAuthToken("com.google", authToken);
authToken = accountManager.blockingGetAuthToken(account,
authTokenType, notifyAuthFailure);
}
catch (OperationCanceledException e) {
}
catch (AuthenticatorException e) {
}
catch (IOException e) {
}
return authToken;
}
Connecting and listing the available tasklists:
public static final String BASE_URL = "https://www.googleapis.com/tasks/v1/users/#me/lists";
public static String AuthUrlEnd() {
return "key=" + Config.GTASKS_API_KEY;
}
public static String AllLists(final String pageToken) {
String result = BASE_URL + "?";
if (pageToken != null && !pageToken.isEmpty()) {
result += "pageToken=" + pageToken + "&";
}
result += AuthUrlEnd();
return result;
}
public String getListOfLists(ArrayList<GoogleTaskList> list)
throws ClientProtocolException, IOException, JSONException {
String eTag = "";
String pageToken = null;
do {
HttpGet httpget = new HttpGet(AllLists(pageToken));
httpget.setHeader("Authorization", "OAuth " + authToken);
// Log.d(TAG, "request: " + AllLists());
AndroidHttpClient.modifyRequestToAcceptGzipResponse(httpget);
try {
JSONObject jsonResponse = (JSONObject) new JSONTokener(
parseResponse(client.execute(httpget))).nextValue();
// Log.d(TAG, jsonResponse.toString());
if (jsonResponse.isNull(NEXTPAGETOKEN)) {
pageToken = null;
}
else {
pageToken = jsonResponse.getString(NEXTPAGETOKEN);
}
// No lists
if (jsonResponse.isNull("items")) {
break;
}
eTag += jsonResponse.getString("etag");
JSONArray lists = jsonResponse.getJSONArray("items");
int size = lists.length();
int i;
// Lists will not carry etags, must fetch them individually if
// that
// is desired
for (i = 0; i < size; i++) {
JSONObject jsonList = lists.getJSONObject(i);
//Log.d("nononsenseapps", jsonList.toString(2));
list.add(new GoogleTaskList(jsonList, accountName));
}
}
catch (PreconditionException e) {
// // Can not happen in this case since we don't have any etag!
// } catch (NotModifiedException e) {
// // Can not happen in this case since we don't have any etag!
// }
}
} while (pageToken != null);
return eTag;
}
Here is how I parse the response:
public static String parseResponse(HttpResponse response)
throws ClientProtocolException, PreconditionException {
String page = "";
BufferedReader in = null;
Log.d(TAG, "HTTP Response Code: "
+ response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() == 403) {
// Invalid authtoken
throw new ClientProtocolException("Status: 403, Invalid authcode");
}
else if (response.getStatusLine().getStatusCode() == 412) { //
/*
* Precondition failed. Object has been modified on server, can't do
* update
*/
throw new PreconditionException(
"Etags don't match, can not perform update. Resolve the conflict then update without etag");
}
/*
* else if (response.getStatusLine().getStatusCode() == 304) { throw new
* NotModifiedException(); }
*/
else if (response.getStatusLine().getStatusCode() == 400) {
// Warning: can happen for a legitimate case
// This happens if you try to delete the default list.
// Resolv it by considering the delete successful. List will still
// exist on server, but all tasks will be deleted from it.
// A successful delete returns an empty response.
// Make a log entry about it anyway though
Log.d(TAG,
"Response was 400. Either we deleted the default list in app or did something really bad");
throw new PreconditionException(
"Tried to delete default list, undelete it");
}
else if (response.getStatusLine().getStatusCode() == 204) {
// Successful delete of a tasklist. return empty string as that is
// expected from delete
Log.d(TAG, "Response was 204: Successful delete");
return "";
}
else {
try {
if (response.getEntity() != null) {
// Only call getContent ONCE
InputStream content = AndroidHttpClient
.getUngzippedContent(response.getEntity());
if (content != null) {
in = new BufferedReader(new InputStreamReader(content));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
page = sb.toString();
//
// System.out.println(page);
}
}
}
catch (IOException e) {
}
finally {
if (in != null) {
try {
in.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
return page;
}