###前提・実現したいこと
AndroidStudioでサーバーのデータベースと接続してデータを保存するアプリを作っています。
android側のデータをphpを介してサーバーのデータベースに保存します。
一度に多数のデータを送信したいです。
###発生している問題・エラーメッセージ
配列に格納されているデータを全て送りたいのですが、for文で回してjsonデータに格納していくと、最後のものしか保存されません。
どのようにすれば一度に配列のデータ全てを送信できますでしょうか。
###該当のソースコード
java
1public class PhpSend extends AsyncTask<String, String, String> { 2 private MainActivity activity; 3 4 public PhpSend(MainActivity activity) { 5 super(); 6 this.activity = activity; 7 } 8 9 @Override 10 protected String doInBackground(String... values) { 11 final StringBuilder result = new StringBuilder(); 12 Globals globals =(Globals)syukkaActivity.getApplication(); 13 URL url = null; 14 String urlSt = "http://10.0.2.2/phpbook/QRsystemSendTest.php"; 15 String readSt = null, str = null; 16 HttpURLConnection con = null; 17 OutputStream os = null; 18 try { 19 url = new URL(urlSt); 20 con = (HttpURLConnection) url.openConnection(); 21 con.setReadTimeout(10000); 22 con.setConnectTimeout(15000); 23 con.setRequestMethod("POST"); 24 con.setRequestProperty("Accept-Language", "ja"); 25 con.setDoInput(true); 26 con.setDoOutput(true); 27 con.setUseCaches(false); 28 con.connect(); 29 30 JSONObject jsonObject = new JSONObject(); 31 for (int i = 0; i < globals.ArrayNum; i++) { 32 jsonObject.put("bango_1", globals.bango_1[i]); 33 jsonObject.put("bango_2", globals.bango_2[i]); 34 jsonObject.put("h_code", globals.h_code[i]); 35 jsonObject.put("h_name", globals.h_name[i]); 36 jsonObject.put("nyusu", globals.nyusu[i]); 37 jsonObject.put("syomi_date", globals.syomi_date[i]); 38 } 39 os = con.getOutputStream(); 40 BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os, "UTF-8")); 41 bufferedWriter.write(String.valueOf(jsonObject)); 42 bufferedWriter.flush(); 43 bufferedWriter.close(); 44 45 os.close(); 46 int status = con.getResponseCode(); 47 48 switch (status) { 49 case HttpURLConnection.HTTP_OK: 50 InputStream is = con.getInputStream(); 51 BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 52 String httpSource = new String(); 53 String string; 54 while (null != (string = reader.readLine())) { 55 httpSource = httpSource + string; 56 } 57 is.close(); 58 break; 59 case HttpURLConnection.HTTP_UNAUTHORIZED: 60 break; 61 default: 62 break; 63 } 64 } catch (MalformedURLException e1) { 65 e1.printStackTrace(); 66 } catch (ProtocolException e1) { 67 e1.printStackTrace(); 68 } catch (IOException e1) { 69 e1.printStackTrace(); 70 } catch (Exception e) { 71 e.printStackTrace(); 72 } finally { 73 if (con != null) { 74 con.disconnect(); 75 } 76 } 77 return readSt; 78 } 79 80 @Override 81 protected void onProgressUpdate(String... values) { 82 // 83 84 } 85 86 @Override 87 protected void onPostExecute(String result) { 88 } 89}
php
1<?php 2$dsn = 'mysql:host=localhost;dbname=qrsystemtest;charset=utf8mb4'; 3$username = 'root'; 4$password = ''; 5 6try{ 7 $pdo = new PDO($dsn,$username,$password); 8 $input_str = file_get_contents("php://input"); 9 $input_json = json_decode($input_str,true); 10 $bango_1 = $input_json["bango_1"]; 11 $bango_2 = $input_json["bango_2"]; 12 $h_code = $input_json["h_code"]; 13 $h_name = $input_json["h_name"]; 14 $nyusu = $input_json["nyusu"]; 15 $syomi_date = $input_json["syomi_date"]; 16 17 // データを取得 18 $stmt = $pdo->prepare("INSERT INTO t_kari_send_his (bango_1, bango_2, h_code, h_name, nyusu, syomi_date) 19 VALUES(:bango_1, :bango_2, :h_code, :h_name, :nyusu, :syomi_date)"); 20 $stmt -> bindParam(':bango_1', $bango_1, PDO::PARAM_STR); 21 $stmt -> bindParam(':bango_2', $bango_2, PDO::PARAM_STR); 22 $stmt -> bindParam(':h_code', $h_code, PDO::PARAM_STR); 23 $stmt -> bindParam(':h_name', $h_name, PDO::PARAM_STR); 24 $stmt -> bindParam(':nyusu', $nyusu, PDO::PARAM_STR); 25 $stmt -> bindParam(':syomi_date', $syomi_date, PDO::PARAM_STR); 26 $stmt -> execute(); 27 28 29}catch (PDOException $e) { 30 echo mb_convert_encoding($e->getMessage(),'UTF-8','SJIS-win'); 31} 32$pdo = null; 33?>
###追記
java側のソース(for文周辺のみ)を修正したものを追記します。
java
1JSONObject jsonObject = new JSONObject(); 2 JSONObject jsonObjectSum = new JSONObject(); 3 JSONObject[globals.ArrayNum]; 4 String name; 5 for (int i = 0; i < globals.ArrayNum; i++) { 6 name = "QR_SOSIN_HIS" + i; 7 jsonObject.put("_id", globals.id[i]); 8 jsonObject.put("bango_1", globals.bango_1[i]); 9 jsonObject.put("bango_2", globals.bango_2[i]); 10 jsonObject.put("h_code", globals.h_code[i]); 11 jsonObject.put("h_name", globals.h_name[i]); 12 jsonObject.put("nyusu", globals.nyusu[i]); 13 jsonObject.put("syomi_date", globals.syomi_date[i]); 14 jsonObjectSum.put(name, jsonObject); 15 Log.d("jsontest", "jsonObjectSumの中身は" + jsonObjectSum); 16 } 17
新たにjsoObjectSumを追加し、そこに動的に作った名前のキーにどんどん追加していくようにしたのですが、キーの名前を動的に変えているのに内容が全て後のものに上書きされてしまいます。(JSONArrayを使ってputしていっても同様)
上書きされずに一個一個保存していきたいのですがどうすれば良いでしょうか?
Logの詳細(jsonObjectSumの中身)は以下の通りです。
D/jsontest: jsonObjectSumの中身は{"QR_SOSIN_HIS0":{"_id":1,"bango_1":"a","bango_2":"a","h_code":"a","h_name":"a","nyusu":"a","syomi_date":"a"}} D/jsontest: jsonObjectSumの中身は{"QR_SOSIN_HIS0":{"_id":2,"bango_1":"ab","bango_2":"ab","h_code":"ab","h_name":"ab","nyusu":"ab","syomi_date":"ab"},"QR_SOSIN_HIS1":{"_id":2,"bango_1":"ab","bango_2":"ab","h_code":"ab","h_name":"ab","nyusu":"ab","syomi_date":"ab"}}
実現したいのは下記のようにidが1のものが一つ目に格納され、二つ目にidが2のものが追加されてほしい
D/jsontest: jsonObjectSumの中身は{"QR_SOSIN_HIS0":{"_id":1,"bango_1":"a","bango_2":"a","h_code":"a","h_name":"a","nyusu":"a","syomi_date":"a"}} D/jsontest: jsonObjectSumの中身は{"QR_SOSIN_HIS0":{"_id":1,"bango_1":"a","bango_2":"a","h_code":"a","h_name":"a","nyusu":"a","syomi_date":"a"},"QR_SOSIN_HIS1":{"_id":2,"bango_1":"ab","bango_2":"ab","h_code":"ab","h_name":"ab","nyusu":"ab","syomi_date":"ab"}}
回答3件
あなたの回答
tips
プレビュー