mirror of
https://github.com/silkimen/cordova-plugin-advanced-http.git
synced 2026-04-05 00:04:16 +08:00
Now able to post with some level of success
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user