HttpURLConnectionを使用して入れ子になったJSONをサーバーにPOSTしたいのですが、応答ステータスは200です。ただし、値はデータベースに入力されていません。私のような別の非同期タスクの)(onPostExecuteにPOSTのための私の非同期タスクを呼び出していますJSONデータがデータベースに入力されていません
{
"id": "C2015300_1481039931",
"data":
{
"customerId": "C2015300",
"arrivedTime": "1481039931",
"storeCode": "100"
}
}
を次のように私のJSONです: -
jsonObject = new JSONObject();
finalJson = new JSONObject();
jsonObject.put("customerId", "C2015300");
Long tsLong = System.currentTimeMillis()/1000;
ts = tsLong.toString();
jsonObject.put("arrivedTime", ts);
Log.d("time", ts);
jsonObject.put("storeCode", storecode);
finalJson.put("id", "C2015300_1481039931");
finalJson.put("data", jsonObject);
UploadTask uptask = new UploadTask();
uptask.execute(String.valueOf(finalJson));
次のようにPOSTのための私の非同期タスクがある: -
public class UploadTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... params) {
String JsonResponse = null;
String JsonDATA = params[0];
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
try {
URL url = new URL("https://qftvufv4m1.execute-api.us-west-2.amazonaws.com/dev/OZCHHX");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
// is output buffer writter
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
Writer writer = new BufferedWriter(new OutputStreamWriter(urlConnection.getOutputStream(), "UTF-8"));
writer.write(JsonDATA);
writer.close();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String inputLine;
while ((inputLine = reader.readLine()) != null)
buffer.append(inputLine + "\n");
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
JsonResponse = buffer.toString();
Log.i("Response",JsonResponse);
return JsonResponse;
} catch (IOException e) {
e.printStackTrace();
}
finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("Error", "Error closing stream", e);
}
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
String status = null;
Log.d("Result",result);
if (pd != null)
{
pd.dismiss();
}
Toast.makeText(getApplicationContext(), result,
Toast.LENGTH_LONG).show();
}
}
このリクエストはどのようにサーバ内にありますか?ログをチェックしましたか? –
サーバーがJSONを受信するかどうかを確認する必要があります。今あなたの質問は '私はバックエンドの問題を抱えているようです、ここに私のクライアント側のコードです。バックエンドを修正するにはどうすればいいですか? –
しかし、私は郵便配達員を使ってチェックしたときにデータがデータベースに入力されました。私のコードに間違いがあるかどうかを知る必要があります。 –