Now able to post with some level of success

This commit is contained in:
Andrew Stephan
2014-03-21 19:10:14 -04:00
parent 7a772d94b0
commit b81f8b3d41
3 changed files with 17 additions and 7 deletions

View File

@@ -57,7 +57,7 @@ public class CordovaHTTP extends CordovaPlugin {
private void setAuthorizationHeaderWithUsernameAndPassword(String username, String password) throws JSONException {
String loginInfo = username + ":" + password;
loginInfo = "Basic " + Base64.encodeToString(loginInfo.getBytes(), Base64.DEFAULT);
loginInfo = "Basic " + Base64.encodeToString(loginInfo.getBytes(), Base64.NO_WRAP);
globalHeaders.put("Authorization", loginInfo);
}

View File

@@ -67,6 +67,7 @@ public class HTTP {
protected void addHeaders(URLConnection conn) throws JSONException {
Iterator<?> i = this.headers.keys();
Log.d(TAG, this.headers.toString(3));
while (i.hasNext()) {
String key = (String)i.next();
conn.setRequestProperty(key, this.headers.getString(key));

View File

@@ -51,12 +51,21 @@ public class HTTPPost extends HTTP implements Runnable {
conn.connect();
int status = conn.getResponseCode();
Log.d(TAG, "The response is: " + status);
is = conn.getInputStream();
String responseData = this.readInputStream(is);
JSONObject response = new JSONObject();
response.put("status", status);
response.put("data", responseData);
callbackContext.success(response);
if (status >= 200 && status < 300) {
is = conn.getInputStream();
String responseData = this.readInputStream(is);
JSONObject response = new JSONObject();
response.put("status", status);
response.put("data", responseData);
callbackContext.success(response);
} else {
is = conn.getErrorStream();
String responseData = this.readInputStream(is);
JSONObject response = new JSONObject();
response.put("status", status);
response.put("error", responseData);
callbackContext.error(response);
}
} catch (MalformedURLException e) {
this.respondWithError(callbackContext, "There is an error with the url");
} catch (JSONException e) {