2017-09-15 127 views
0

我想將arraylist從android讀取到php以便存儲在database中,但我無法找到它的確切代碼。任何人都可以指導我解決這個問題嗎?php:讀取json arraylist並將數據保存在mysql中?

下面是創建arraylist

private void loadCart() 
{ 

    productList.clear(); 
    Cursor cursor = dbHelper.getCarProducts(); 
    cursor.moveToFirst(); 
    do { 
     CartProduct cartProduct = new CartProduct(); 
     cartProduct.setProductName("Name: "+cursor.getString(cursor.getColumnIndex("_Name"))); 
     cartProduct.setProductCost("Cost: "+cursor.getString(cursor.getColumnIndex("_Cost"))); 
     cartProduct.setProductPrice("Price: "+cursor.getString(cursor.getColumnIndex("_Price"))); 
     cartProduct.setProductQuantity("Quantity: "+cursor.getString(cursor.getColumnIndex("_Quantity"))); 
     productList.add(cartProduct); 

    }while(cursor.moveToNext()); 


} 

我使用retrofit2以發送ArrayList的服務器我的Java代碼,但正如我在這裏的其他問題所看到的,我不能夠獲取file_get_contents的url?

+0

請顯示您的代碼 – Pritamkumar

+0

@Pritamkumar這是Java和我無法開始與PHP代碼 –

+0

你究竟想做什麼,是你想發送名稱,成本...數據類型的服務器或你想接收並將其存儲到數據庫中。如果你想發送你的數據到服務器,然後使用改進,因爲你提到的,但首先你有Web服務來接收它,如果你給我這種什麼樣的數據格式,它將能夠接收,然後我能夠指導你。 –

回答

0

解析在phpjson值後的幾乎搜索了3個星期以及n +小時的沮喪,我終於找到了解決我的問題的辦法,並與人員分享,以便他們能夠從中受益(因爲我被阻止從另一個a/c因爲要求太麻煩爲了將你的數組列表發送到你的服務器,我們需要使用另一個庫,根據我的需要,最適合的就是循環j中的async-http庫,這些是導入和使用庫的步驟在你的程序: -

1)導入庫到您的項目在您的bulid.gradle寫這篇聲明(應用模塊): -

編譯「com.loopj.android:android-async-http :1.4.9'

2)爲了在你的程序中使用它們創建以下變量: -

AsyncHttpClient (a client to send data to your server) 
    RequestPrarms (Data sent to your server for parsing and further operations) 
    String url(Link to your server where actually operations occurs) 

3。)現在我們用這些變量並運行該程序: -

p

arams.put("OrderSummary", <your array list>); 
      httpClient.post(APIURL.API_URL,params, new AsyncHttpResponseHandler() { 
       @Override 
       public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { 
        //your operations on success 
       } 

       @Override 
       public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { 
        Toast.makeText(getApplicationContext(),error.toString(),Toast.LENGTH_LONG).show(); 
       } 
      }); 

我希望這會清除你的疑慮有所我仍然創造我的PHP側,以在數據庫中插入數據

0

在這裏你去...

第1步:添加改裝依賴於你的gradle.app

compile 'com.squareup.retrofit:retrofit:1.9.0' 

第2步:製作一個RESTClient實現類像下面。

public class RestClient { 
    private static final String BASE_URL = DataConstants.TEST_URL; //Place your web service URL here 
    private ApiInterface apiService; 

    public RestClient() 
    { 
     RequestInterceptor requestInterceptor = new RequestInterceptor() { 
      @Override 
      public void intercept(RequestFacade request) { 

       request.addHeader("Accept", "application/json"); 
      } 
     }; 

     RestAdapter restAdapter = new RestAdapter.Builder() 
       .setLogLevel(RestAdapter.LogLevel.FULL) 
       .setRequestInterceptor(requestInterceptor) 
       .setEndpoint(BASE_URL) 
       .build(); 

     apiService = restAdapter.create(ApiInterface.class); 
    } 

    public ApiInterface getApiService() 
    { 
     return apiService; 
    } 
} 

第3步:爲POST URL創建一個接口。

public interface ApiInterface { 
    @POST("/sendData") 
    void sendData(@Body JsonObject jsonObject, 
          Callback<DataModel> dataModelCallback); 
} 

第4步:製作一個POJO類,如下所示。

public class DataModel{ 
    private String success; 

public String getSuccess() { 
     return success; 
    } 

public void setSuccess(String success) { 
     this.success = success; 
    } 

} 

步驟5:從您的活動中調用webservice,如下所示。

private void callWebService(String user_id) { 
     try {//TODO SEND 
      final Utility utility = new Utility(this); 
      utility.showProgressDialog(); 
      JsonObject myJsonData = new JsonObject(); 
      myJsonData.addProperty("user_id", user_id); 
      Gson gsonData = new GsonBuilder().create(); 
      JsonArray dataArray = new JsonArray(); 
      dataArray = gsonData.toJsonTree(productList).getAsJsonArray(); //Here you want to add your array list i.e productList 
      myJsonData.add("assign_to", jaAssignee); 

      new RestClient().getApiService().sendData(myJsonData, new Callback<DataModel>() { 
       @Override 
       public void success(DataModel dataModel, Response response) { 
        utility.hideProgressDialog(); 
        try { 
         String success = dataModel.getSuccess(); 
         if (success.equalsIgnoreCase("Success")) { 
          //Do what you want to do 
         } 
        } catch (Exception e) { 
        } 
       } 

       @Override 
       public void failure(RetrofitError error) { 
        utility.hideProgressDialog(); 
       } 
      }); 
     } catch (Exception e) { 
     } 
    } 

希望這會幫助你!

+0

我需要php腳本來解析我的數組/列表以便將數據存儲在數據庫中 –

+0

好的沒問題。 –

0

一次性發送所有數據。創建JsonArray並通過創建JsonObject來添加每個對象。完成後,一次性上傳所有數據。你只需要在php中解碼該數組。

優勢利用這一點,你可以管理改造的響應非常好(即它異步)

+0

可以給我解析json數組的php腳本 –

+0

你可以在任何地方找到它。在php中搜索json_decode函數。 – The90sArtist

0

您可以通過

<?php 
    header('Content-type: application/json'); 
    $jsonString = file_get_contents('php://input'); 
    $jsonArray = json_decode($jsonString,true); 

    print_r($jsonArray);/* print array */ 

?>

+0

for file_get_contents應該給哪個鏈接? –

+0

沒有鏈接是必需的,因爲您期望數據從'android'通過'json' data – Pritamkumar

+0

它可以與arraylist一起使用嗎? –