나는 안드로이드 개발에 매우 익숙하다. 그래서 내 시도가 100 % 정확하지 않으면 변명한다.안드로이드에서 REST 서비스 : 400 예외
JavaScript에서는 기본 인증을 사용하여 WCF 서비스에 연결하여 요청한 PDF 문서의 base64 인코딩 문자열을 검색하는 기능이 있습니다. 자바 스크립트는 아래의 작동 :
function svcPost(param) {
return jQuery.ajax({
type: 'POST',
url: 'https://url-to-service.svc/' + param.method,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
data: JSON.stringify(param.json),
async: true,
processData: true,
crossDomain: true,
beforeSend: function (request) {
request.setRequestHeader("Authorization", "Basic " + window.localStorage.getItem('credentials'));
},
success: function(result) {
return result;
},
error: function (xhr, status, error) {
return error;
}
});
};
이 기능은 다음 상기 base64로 인코딩 된 문자열 반환 약속 호출 할 것이다 : 나는 안드로이드 응용 프로그램이 복제하려고
jQuery.when(
svcPost({ method: 'GetBase64Pdf', json: { id: lookupId } })
)
.done(function (result) {
deferred.resolve(result[0]);
})
.fail(function(error) {
console.error(error);
});
및 나는 운이 없다. 이 서비스에 대한 모든 호출은 400 BAD REQUEST를 리턴하는데, 호출은 'value'를 가진 'type'을 기대하지만 null을 발견했다.
'dataType :'json ''과 관련이 있다는 것을 알고 있지만 안드로이드에서는 작동하지 않습니다.
내가 지금까지 가지고있는 코드 :
byte[] bytes = "Username:Password1".getBytes("UTF-8");
String encoded = Base64.encodeToString(bytes, 0);
String itemid = Integer.toString(lookupId);
String param = "id=" + URLEncoder.encode(itemid, "UTF-8");
String url = "https://url-to-service.svc/GetBase64Pdf";
URL uri = new URL(url);
HttpURLConnection connection = (HttpURLConnection)uri.openConnection();
try {
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
connection.setRequestProperty("Content-Length", Integer.toString(param.getBytes().length));
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Authorization", "Basic " + encoded);
connection.setDoOutput(false);
connection.setUseCaches(false);
connection.setDoInput(true);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(param);
wr.flush();
wr.close();
InputStream in = null;
int status = connection.getResponseCode();
if (status >= HttpStatus.SC_BAD_REQUEST) {
in = connection.getErrorStream();
}
else {
in = connection.getInputStream();
}
BufferedReader rd = new BufferedReader(new InputStreamReader(in));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
내가 'dataType와 :'에 대한 헤더 항목으로 '이 동의'를 추가 시도 JSON ''하지만이 작동하지 않습니다.
'& type = json'을 매개 변수 문자열에 추가하면 오류 메시지가 json 배열로 반환됩니다.
결석이 있습니까? WCF 서비스에 액세스 할 수 없으며 클라이언트 환경에서 타사 호출이므로 서비스 자체를 확인할 수 없습니다. JavaScript는 작동하지만 Android에서 JavaScript 구현을 복제 할 수는 없습니다.
어떤 도움을 크게 감상 할 수, 나는 내가 읽은 블로그가 어떤 성공을 :(없었다 않은 시간이 찢는 된