これはサーバーへのPOSTリクエストです-
public String callServiceTotalRecords(String userName, String password, String email, String type, String start, String end, String userTimeZone, JSONArray ContentClassArr)
{
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(WEBSERVICE + type);
HttpResponse response = null;
String responseBody = "";
try {
String base64EncodedCredentials = "Basic " + Base64.encodeToString(
(userName + ":" + password).getBytes(),
Base64.NO_WRAP);
httppost.setHeader("Authorization", base64EncodedCredentials);
httppost.setHeader(HTTP.CONTENT_TYPE,"application/json");
JSONObject obj = new JSONObject();
obj.put("Start", start);
obj.put("End", end);
obj.put("emailId", email);
obj.put("userTimeZone", userTimeZone);
obj.put("ContentClassArr",ContentClassArr.toString());
httppost.setEntity(new StringEntity(obj.toString(), "UTF-8"));
// Execute HTTP Post Request
response = httpclient.execute(httppost);
if (response.getStatusLine().getStatusCode() == 200)
{
responseBody = EntityUtils.toString(response.getEntity());
Log.d("response ok", "ok response :/");
}
else
{
responseBody = "";
Log.d("response not ok", "Something went wrong :/");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
catch (JSONException e) {
e.printStackTrace();
}
return responseBody;
}
しかし、応答は404 notfoundです。
"response not ok: Something went wrong"
疑問は、次のように形成されたタイプJSONArrayのContentClassArrにあります-
JSONArray ContentClassArr= new JSONArray("[\"UserLog\",\"Sheets\"]");
それから私はそれを次のようにJSONObjectに入れています-
obj.put("ContentClassArr",ContentClassArr.toString());
サーバー上の一般的な正しいjsonは次のようになります-
{"emailId":"[email protected]","Start":"2014-01-09T12:51:34.110Z","userTimeZone":"America/Los_Angeles","End":"2014-01-16T12:51:34.110Z","ContentClassArr":["UserLog","Sheets"]}
それはパッティングの正しい方法であるJSONArray
にJSONObject
どこか別の場所であるか、エラー?
obj.put("ContentClassArr",ContentClassArr.toString());
を呼び出すのtoString()
ではなくJSONArray
、そのまま渡すだけです。
obj.put("ContentClassArr", ContentClassArr);
そうは言っても、それはあなたの問題ではありません。404
POSTからのAは、URLが正しくないことを意味します。ただし、適切なURLを使用すると、JSONが問題になります。
また、大文字の変数名は使用しないでください。これは命名規則に反し、コードを読みにくくします。クラス名は大文字ですが、変数は大文字ではありません。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加