0

このコードを使用して、ローカルのsqliteデータベースからサーバー(mysql)に情報を送信すると、正常に機能します。しかし、私はtable1からのみデータを送信します。すべてのsqliteテーブルのデータをサーバーに送信したい場合、どうすればよいですか?

public class sendInformation  {
public SQLiteDatabase newDB;
JSONObject jObject;
JSONArray jArray = new JSONArray();

public void getValues()
{
    try
    {
        newDB = SQLiteDatabase.openDatabase("/data/data/com..../databases/...db",null,SQLiteDatabase.CONFLICT_NONE);    
    }catch(SQLException e)
    {
        Log.d("Error","Error while Opening Database");
        e.printStackTrace();
    }
    try
    {
        Cursor c = newDB.rawQuery("Select * from table1",null);
        if (c.getCount()==0) 
        {
            c.close();
            Log.d("","no items on the table");              
        }else
        {
            c.moveToFirst();    

            while(c.moveToNext()) {
                jObject = new JSONObject();
                jObject.put("ID", c.getString(c.getColumnIndex("ID")));
                jObject.put("Notes", c.getString(c.getColumnIndex("Notes")));
                jObject.put("Cellphone", c.getString(c.getColumnIndex("Cellphone")));
                jObject.put("Date", "null");
                jObject.put("Address", "null");
                jArray.put(jObject);
            }

            c.close();
            Log.d("","ALL the data in the DB"+jArray.toString());
            int arrayLength=jArray.length();
            Log.d("","Lenght of the jArray"+arrayLength);
            HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams,9000);
            HttpConnectionParams.setSoTimeout(httpParams, 9000);

            HttpClient client = new DefaultHttpClient(httpParams);

            String url = "http://ipaddress/..../test.php?arrayLength="+arrayLength;

            HttpPost request = new HttpPost(url);
            request.setEntity(new ByteArrayEntity(jArray.toString().getBytes("UTF8")));      
            request.setHeader("json", jArray.toString());

            HttpResponse response = client.execute(request);
            HttpEntity entity = response.getEntity();
            // If the response does not enclose an entity, there is no need

            if (entity != null) {
                InputStream instream = entity.getContent();

                String result =  RestClient.convertStreamToString(instream);
                Log.i("Read from server", result); 
            }
        }       
    } catch (UnsupportedEncodingException uee) {
        Log.d("Exceptions", "UnsupportedEncodingException");
        uee.printStackTrace();
    }catch (Throwable t) {
        Log.d("","request fail"+t.toString());
    }

    this.newDB.close(); 
  }
}

ご覧のとおり、私はすべてのデータを取得しtable1、jsonObjectを作成します。

  • それをtest.phpファイルに渡しますが、table2、table3 ...から別のクエリを実行する場合、2番目と3番目のテーブルの値を同じJsonObjectに配置する必要がありますか?
  • そうすると、どのデータがどのテーブルからのものであるかをどのように知ることができますか?
  • 複数のjsonobjectを送信できる場合、それを行う方法と、test.phpファイルでそのデータを受け取る方法を教えてください。

今私はそれをします$json = file_get_contents('php://input');

4

1 に答える 1

0

あなたはすでに半分の解決策を持っています。追加のテーブルごとに JsonObject を作成して入力するコードを複製し、それらを JsonArray に追加するだけです。JsonArray の代わりに、別の JsonObject を使用できます。これには、他の JsonObject を含めることができます。

サーバーでは、json_decode($json, true) を使用するだけで、データを配列の配列として取得できます。結果を print_r() でファイルにダンプして、データの構造を確認できます。

于 2013-02-08T00:42:40.593 に答える