Android 앱에서 Apache HTTP에서 HttpUrlConnection으로 이동하려고합니다. 나는 붙어있어 어디서나 볼려고했지만 나는 그것을 통과 할 수 없다. 여기 제가 시도하고있는 것이 있습니다. 내가 위를하려고하면HTTPPost 대 Android의 HttpUrlConnection POST
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
for (Entry<String, String> entry: parameters.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
String queryString = URLEncodedUtils.format(nvps, HTTP.UTF_8);
String modUrl = baseUrl + "?" + queryString;
HttpURLConnection urlConnection = null;
try {
URL url1 = new URL(modUrl);
urlConnection = (HttpURLConnection) url1.openConnection();
CookieManager cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
cookieManager.getCookieStore().add(new URI(url), podCookie);
urlConnection.setConnectTimeout(60000);
urlConnection.setReadTimeout(60000);
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Authorization", <auth-header>);
urlConnection.setRequestProperty("Content-Type", "application/json");
OutputStream os = urlConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(<String JSON to send>);
writer.flush()
writer.close();
os.close();
InputStream is = new BufferedInputStream(urlConnection.getInputStream());
String responseString = WebService.convertInputStreamToString(is);
, 나는이 401 Unauthorized error
를 얻을 :
HttpParams timeoutParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(timeoutParams, 60000);
HttpConnectionParams.setSoTimeout(timeoutParams, 60000);
DefaultHttpClient httpClient = null;
httpClient = new DefaultHttpClient(timeoutParams);
Cookie podCookie = getPodCookie();
if (podCookie != null) {
httpClient.getCookieStore().addCookie(podCookie);
}
HttpPost postMethod = null;
postMethod.addHeader("Authorization", "<auth-header>");
try {
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
for (Entry<String, String> entry : parameters.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
}
String queryString = URLEncodedUtils.format(nvps, HTTP.UTF_8);
String modUrl = url + "?" + queryString;
postMethod = new HttpPost(modUrl);
StringEntity entity = new StringEntity(<String JSON to send>, HTTP.UTF_8);
postMethod.setEntity(entity);
postMethod.setHeader("Content-type", "application/json");
HttpResponse reply = httpClient.execute(postMethod);
이것은 위의 코드의 HttpURLConnection의 동등한은 다음과 같습니다
다음은 내 HTTP 코드입니다. Charles
을 사용 중이며 머리글은 같습니다.
URL url1 = new URL(baseUrl);
와 같이, 라이터에 다음 줄을 추가합니다 :
이 같은 대신 내가 기본 URL의 URL을 변경하려면 URL의 BufferedWriter
의 검색어 매개 변수를 추가하려고
writer.write(modUrl)
이렇게하면 500 Internal Server Error
이됩니다.
두 경우 모두 IOException
이 FileNotFoundException
이고 InputStream
줄에 있습니다.
해결 방법에 대한 아이디어가 있으십니까?