2016-07-30 14 views
0

私は値の文字列array = {6,37,38,837,7,...}を含んでいる文字列配列を持っており、いくつかの文字列値も使用しています。androidでphpを使用してサーバーにString Arrayデータを送信する方法

すべての文字列値と文字列配列をサーバー側のデータベースに送信します。送信ボタンをクリックすると、データがテーブルに保存されます。

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

service callにはRetrofit apiを使用できます。

Retrofitは、AndroidおよびJava用のRESTクライアントでSquareです。これにより、 は、JSON(または他の構造化データ) をRESTベースのWebサービスを介して取得およびアップロードするのが比較的容易になります。

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

Retrofitハンドル自動的に配列、文字列json

それを試してみてください:レトロフィットでは、詳細についてはデータのシリアル化

のために使用されるコンバータ 設定します!!

+0

どのように私はそれを使用することができます。そのページには何も表示されません。正確なリンクを提供してください – user6657179

+0

こんにちは@ user6657179、今すぐ確認してください!! –

+0

それはいいですが、私は単純なものをしたい..私は作成して送信するには大きすぎるプロセスがあります。ありがとうございます。最も簡単な解決策が見つからない場合は、私はそれを使用します。 – user6657179

0

ループを試すSomething like this. ポストはボレー用ですが、コード内でこれを行う方法と同じ方法です。

+0

私はdoInBackgroundでこのコードを使用するか、クラス内に新しいメソッドを作成できます。私は混乱しています – user6657179

+0

その後にサーバーを送信する方法 – user6657179

+0

nullポインターのエラーが消えましたエラーはデータの解析中エラーですorg.json.JSONException:型java.lang.Stringの値 user6657179

関連する問題