实际上,我正在android中做一个应用程序。现在,我必须将微调器,范围日期选择器以及编辑文本数据的这些数据发送到API,并且应该会收到响应。我该怎么办?
我已经尝试过,但没有得到任何输出。
我已经尝试过此代码。
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import com.Mobepay.Ewallet.Constants;
import com.Mobepay.Ewallet.R;
import com.Mobepay.Ewallet.activity.hotel.Result_page_travelumActivity;
import com.Mobepay.Ewallet.app.Config;
import com.Mobepay.Ewallet.app.MyApplication;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.squareup.timessquare.CalendarPickerView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Document;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class activity_book_hotel extends AppCompatActivity {
private ProgressDialog pDialog1;
private PrefManager prefnew;
private ProgressDialog pDialog;
private SharedPreferences pref;
EditText city_id;
private String first_date, last_date;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book_hotel);
city_id = findViewById(R.id.city_id);
city_id.getText().toString();
ArrayAdapter<CharSequence> adapteradult;
ArrayAdapter<CharSequence> adapterchild;
ArrayAdapter<CharSequence> adapterroom;
String[] array_adult = {"1adult", "2adult", "3adult", "4adult"};
String[] array_child = {"0child", "1child", "2child", "3child", "4child"};
String[] array_rooms = {"1room", "2room", "3room", "4room"};
Spinner adult_spinner = findViewById(R.id.adult_count);
Spinner child_spinner = findViewById(R.id.child_count);
Spinner room_spinner = findViewById(R.id.room_val);
adapteradult = new ArrayAdapter<>(this,android.R.layout.simple_spinner_item,array_adult);
adapteradult.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adult_spinner.setAdapter(adapteradult);
adapterchild= new ArrayAdapter<>(this,android.R.layout.simple_spinner_item,array_child);
adapterchild.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
child_spinner.setAdapter(adapterchild);
adapterroom= new ArrayAdapter<>(this,android.R.layout.simple_spinner_item,array_rooms);
adapterroom.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
room_spinner.setAdapter(adapterroom);
String selectedadult = adult_spinner.getSelectedItem().toString();
String selectedchild = child_spinner.getSelectedItem().toString();
String selectedroom = room_spinner.getSelectedItem().toString();
System.out.println(selectedadult+" "+selectedchild+" "+selectedroom);// check the output in logcat
getResources().getStringArray(R.array.array_adults);
getResources().getStringArray(R.array.array_child);
getResources().getStringArray(R.array.array_rooms);
Button btprcd = findViewById(R.id.btproced);
btprcd.setOnClickListener(v -> {
get_token_id();
});
final CalendarPickerView calendar_view = findViewById(R.id.calendar_view);
Calendar nextYear = Calendar.getInstance();
nextYear.add(Calendar.YEAR, 1);
Date today = new Date();
calendar_view.init(today, nextYear.getTime())
.inMode(CalendarPickerView.SelectionMode.RANGE);
calendar_view.setOnDateSelectedListener(new CalendarPickerView.OnDateSelectedListener() {
@Override
public void onDateSelected(Date date) {
String first_date = calendar_view.getSelectedDates().get(0).toString();
String last_date = calendar_view.getSelectedDates().get(calendar_view.getSelectedDates().size()-1).toString();
}
@Override
public void onDateUnselected(Date date) {
Toast.makeText(getApplicationContext(),"Select Check in and Check out dates",Toast.LENGTH_SHORT).show();
}
});
}
private void get_token_id() {
StringRequest strReq = new StringRequest(Request.Method.POST,
Config.api_url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject responseObj = new JSONObject(response);
boolean error = responseObj.getBoolean("error");
String message = responseObj.getString("message");
if (!error) {
responseObj.getJSONArray("token_details");
String city_id = responseObj.getString("city_id");
String check_in = responseObj.getString("check_in");
String check_out = responseObj.getString("check_out");
String room_val = responseObj.getString("room_val");
String adult_val = responseObj.getString("adult_val");
String child_val = responseObj.getString("child_val");
String adult_count = responseObj.getString("adult_count");
String child_count = responseObj.getString("child_count");
String child_age1 = responseObj.getString("child_age1");
String child_age2 = responseObj.getString("child_age2");
String ChildAge = responseObj.getString("child_age");
prefnew.set_hotel_city_id(city_id);
prefnew.set_hotel_check_in(check_in);
prefnew.set_hotel_check_out(check_out);
prefnew.set_hotel_room_val(room_val);
prefnew.set_hotel_adult_val(adult_val);
prefnew.set_hotel_child_val(child_val);
prefnew.set_hotel_adult_count(adult_count);
prefnew.set_hotel_child_count(child_count);
prefnew.set_hotel_child_age1(child_age1);
prefnew.set_hotel_child_age2(child_age2);
prefnew.set_hotel_ChildAge(ChildAge);
MyApplication.getInstance().cancelPendingRequests(this);
} else {
if (pDialog.isShowing())
pDialog.dismiss();
Toast.makeText(activity_book_hotel.this.getApplicationContext(), message, Toast.LENGTH_SHORT).show();
if(message.equals("Sorry, We are not able to authenticate your device."))
{
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean(Constants.IS_LOGGED_IN,false);
editor.putString(Constants.EMAIL,"");
editor.putString(Constants.NAME,"");
editor.putString(Constants.UNIQUE_ID,"");
editor.apply();
editor.commit();
prefnew.setispincorrect(false);
Intent myIntent = new Intent(activity_book_hotel.this, Result_page_travelumActivity.class);
startActivity(myIntent);
}
}
if (pDialog.isShowing())
pDialog.dismiss();
} catch (JSONException e) {
if (pDialog.isShowing())
pDialog.dismiss();
Toast.makeText(activity_book_hotel.this.getApplicationContext(), "Sorry! Please Try again later."+e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}, error -> {
if (pDialog.isShowing())
pDialog.dismiss();
Toast.makeText(activity_book_hotel.this.getApplicationContext(), "Please your network connection.", Toast.LENGTH_SHORT).show();
}) {
/**
* Passing user parameters to our server
* @return
*/
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("city_id", "city_id");
params.put("first_date", "check_in");
params.put("last_date", "check_out");
params.put("room_val", "room_val");
params.put("adult_count", "adult_count");
params.put("child_count", "child_count");
params.put("child_age1", "child_age1");
params.put("child_age2", "child_age2");
params.put("child_age", "child_age");
return params;
}
};
int socketTimeout = 60000;
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
strReq.setRetryPolicy(policy);
strReq.setTag(this);
strReq.setShouldCache(false);
MyApplication.getInstance().cancelPendingRequests(this);
MyApplication.getInstance().addToRequestQueue(strReq);
}
}
我必须将这些数据作为字符串发布到API,并且应该会收到响应。