http 応答を取得するためにさまざまな方法を試しましたが、常にメッセージが null になります。Chrome に付属の Advance Rest Client ツールを使用してみました。その中で、成功した応答を取得します。どこが間違っているのかわかりません。
try {
String urlParameters = "id=userid&password=password&device=android";
URL url = new URL("my url");
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
urlConnection.setRequestProperty("Content-Length",
"" + Integer.toString(urlParameters.getBytes().length));
urlConnection.setRequestProperty("Content-Language", "en-US");
urlConnection.setUseCaches(false);
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
OutputStream outputStream = urlConnection.getOutputStream();
outputStream.write(urlParameters.getBytes());
outputStream.close();
int responseCode = urlConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream in = urlConnection.getInputStream();
resultstring = convertinputStreamToString(in);
Log.d("Result String-------->", resultstring);
}
} catch (Exception e) {
e.printStackTrace();
}
結果が得られるスクリーンショットを添付しました:成功しかし、上記のコードでは結果が得られます:失敗
HttpClient と HttpPost を使用してコードも試しました。しかし、これも機能しません
try {
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(
"my url");
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("id", "userid"));
nameValuePairs.add(new BasicNameValuePair("password", "password"));
nameValuePairs.add(new BasicNameValuePair("device", "android"));
// httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
UrlEncodedFormEntity se = new UrlEncodedFormEntity(nameValuePairs);
se.setContentType("application/x-www-form-urlencoded");
httpPost.setEntity(se);
httpPost.getParams().setBooleanParameter(
"http.protocol.expect-continue", false);
HttpResponse response = client.execute(httpPost);
String responseAsText = EntityUtils.toString(response.getEntity());
Log.d("Result String-------->", responseAsText);
} catch (Exception e) {
e.printStackTrace();
}
私を助けてください。私はほとんどすべてを試しました。