2016-07-30 53 views
0

我有一個字符串數組,其中包含值字符串array = {6,37,38,837,7,...},我也使用了一些字符串值。如何發送字符串數組到服務器使用php在android

我想將所有字符串值和字符串數組發送到位於服務器端的數據庫。當我點擊提交按鈕時,數據將存儲在表格中。

在PHP方面,我想讀取這個數組,因爲它是,我會在後臺做的其他事情意味着PHP方面。看到我的下面的代碼,那麼你會得到它所說的。

這裏是我的代碼:

public class Hotels extends Activity { 
// Declare Variables 
JSONArray jsonarray = null; 
private static final String TAG_ID = "id"; 
public static final String TAG_NAME = "name"; 
public static final String TAG_LOCATION = "location"; 
public static final String TAG_DESC = "description"; 
String f_date, l_date; 
ArrayList<String> ne = new ArrayList<String>(); 
public Set<String> interestList = new HashSet<String>(); 
public Set<String> interestlist = new HashSet<String>(); 
//public String[] hotel_id = new String[0]; 

ProgressDialog loading; 
ListView list; 
Button booknow; 
private ArrayList<Product> itemlist; 
Product product; 
static String Array = "MyHotels"; 
View view; 
CheckBox click; 
String hotel = "http://app.goholidays.info/getHotelData.php"; 
String booking = "http://app.goholidays.info/insertIntoBooking.php"; 
SharedPreferences sp; 
SharedPreferences.Editor editor; 


@Override 
protected void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.all_item_layout); 
    product = new Product(); 
    itemlist = new ArrayList<Product>(); 
    new ReadJSON().execute(); 
    click = (CheckBox) findViewById(R.id.mycheckbox); 
    booknow = (Button) findViewById(R.id.bookhotel); 
    product = new Product(); 
    list = (ListView) findViewById(R.id.myimagelist); 

    Calendar c = Calendar.getInstance(); 
    System.out.println("Current time => " + c.getTime()); 
    SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); 
    sp = PreferenceManager.getDefaultSharedPreferences(Hotels.this); 
    editor = sp.edit(); 

    final String user_id = sp.getString("id", TAG_ID); 
    final String start_date = sp.getString("start_date", f_date); 
    final String end_date = sp.getString("end_date", l_date); 
    final String chk_status = df.format(c.getTime()); 

    booknow.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String[] hotel_id = new String[ne.size()]; //here is my string array{1,2,2,3,4,...} i want send it to server as it is. 
      hotel_id = ne.toArray(hotel_id); 

      for(int i = 0; i < hotel_id.length ; i++){ 
       Log.d("string is",(String)hotel_id[i]); 
      } 

      new BackTask().execute(); 
     } 
    }); 
} 

.......... 

    class BackTask extends AsyncTask<String, Void, String> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
     } 

     @Override 
     protected String doInBackground(String... params) { 

List<NameValuePair> param = new ArrayList<NameValuePair>(); 
      param.add(new BasicNameValuePair("start_date", start_date)); 
      param.add(new BasicNameValuePair("end_date", end_date)); 
      param.add(new BasicNameValuePair("chk_status", chk_status)); 
      for(String value: hotel_id){ 
       param.add(new BasicNameValuePair("hotel_id[]",value)); 
      } 
      param.add(new BasicNameValuePair("user_id", user_id)); 

      JSONObject json = jsonParser.makeHttpRequest(booking, "POST", param); 

      Log.d("Create Response", json.toString()); 

      // check for success tag 
      try { 
       int success = json.getInt(TAG_SUCCESS); 

       if (success == 1) { 
        // successfully book a hotels 
        Intent i = new Intent(getApplicationContext(), HomePage.class); 
        startActivity(i); 

        // closing this screen 
        finish(); 
       } else { 
        // failed to book hotels 
        Log.d("failed to book hotel", json.toString()); 

       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 
      if(result != null){ 
       Toast.makeText(getApplicationContext(), "Book Success", Toast.LENGTH_SHORT).show(); 
      } 
     } 

    } 
    BackTask lg = new BackTask(); 
    //here the array is converted in to string 
    lg.execute(start_date, end_date, chk_status, String.valueOf(hotel_id), user_id); 
} 
} 

看到我的logcat

JSON Parser: Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject 

這裏是我的PHP代碼

<?php 

require "db_connect.php"; 

$response = array(); 

// check for required fields 
if (isset($_POST['start_date']) && isset($_POST['end_date']) &&  isset($_POST['user_id']) && isset($_POST['hotel_id']) && isset($_POST['chk_status'])) { 

$start_date= $_POST["start_date"]; 
$end_date= $_POST["end_date"]; 
$user_id= $_POST["user_id"]; 
$hotel_id= $_POST["hotel_id"]; 
$chk_status= $_POST["chk_status"]; 

$result= mysqli_query("insert into tb_booking(start_date,end_date,user_id,hotel_id,chk_status) values ('$start_date','$end_date','$user_id','$hotel_id','$chk_status')"); 

if ($result) { 
    // successfully inserted into database 
    $response["success"] = 1; 
    $response["message"] = "Hotel book successfully."; 

    // echoing JSON response 
    echo json_encode($response); 
} else { 
    // failed to insert row 
    $response["success"] = 0; 
    $response["message"] = "Oops! An error occurred.Please select hotels"; 

    // echoing JSON response 
    echo json_encode($response); 
} 
}else { 
// required field is missing 
$response["success"] = 0; 
$response["message"] = "Required field(s) is missing"; 

// echoing JSON response 
echo json_encode($response); 
}  
?> 

請幫我解決這個問題。

+1

代碼太多。你真的期望有人經歷這一切嗎? – m02ph3u5

+0

不要直接去底部提交方法的代碼。 – user6657179

+0

是的,我會解決它。謝謝 – user6657179

回答

3

您可以使用Retrofit api進行服務呼叫。

改造是Square的Java和REST客戶端。它使得它 通過基於REST的web服務檢索和上傳JSON(或其他結構化數據) 相對容易。在改造配置該轉換器 用於數據序列化

欲瞭解更多信息:

http://www.androidhive.info/2016/05/android-working-with-retrofit-http-library

Retrofit手柄自動數組,字符串json

試試吧。!

+0

我該如何使用它。他們沒有顯示在該頁面上。請提供一個確切的鏈接 – user6657179

+0

Hello @ user6657179,現在檢查。 –

+0

這很好,但我想要最簡單的一個..我有太大的過程來創建和發送。謝謝如果找不到最簡單的解決方案,我會使用它。 – user6657179

0

嘗試循環Something like this. 該鏈接提供的鏈接是排除的,但您可以在代碼中執行此操作。

+0

我可以在doInBackground中使用此代碼或在類中創建新方法。我很困惑 – user6657179

+0

之後,如何發送它服務器 – user6657179

+0

空指針錯誤消失了現在錯誤是解析數據錯誤org.json.JSONException:值的類型java.lang.String不能轉換爲JSONObject – user6657179

相關問題