Java 从数据库填充微调器,检索所选微调器值并保存到SQL DB
我正在构建一个公交车位置应用程序。我正在尝试从微调器获取选定值。请注意,微调器是从DB填充的,但是,当我尝试获取微调器的选定值时,我的应用程序崩溃。我想从微调器获取所选值并将其发送到我的DBJava 从数据库填充微调器,检索所选微调器值并保存到SQL DB,java,php,android,Java,Php,Android,我正在构建一个公交车位置应用程序。我正在尝试从微调器获取选定值。请注意,微调器是从DB填充的,但是,当我尝试获取微调器的选定值时,我的应用程序崩溃。我想从微调器获取所选值并将其发送到我的DB public class DriversLocationUpdate extends AppCompatActivity implements AdapterView.OnItemSelectedListener{ ArrayList<String> listitems1= new
public class
DriversLocationUpdate extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
ArrayList<String> listitems1= new ArrayList<> ();
ArrayAdapter<String> adapter1;
ArrayList<String> listitems2= new ArrayList<> ();
ArrayAdapter<String> adapter2;
Spinner busListArray, locationListArray;
ProgressBar progressbar;
Button btnUpd;
String currselectedBus, logstatus, currselectedLocat;
TextView vtxtValidation;
TextView res1,res2;
//String[] arrayUserinfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drivers_location_update);
progressbar = (ProgressBar) findViewById(R.id.loginprogress);
res1=(TextView) findViewById(R.id.spin1);
res2=(TextView) findViewById(R.id.spin2);
busListArray = (Spinner) findViewById(R.id.bus_arrays);
adapter1 = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, listitems1);
busListArray.setAdapter(adapter1);
locationListArray = (Spinner) findViewById(R.id.location_arrays) ;
adapter2 = new ArrayAdapter<>(this, R.layout.support_simple_spinner_dropdown_item, listitems2);
locationListArray.setAdapter(adapter2);
busListArray.setOnItemSelectedListener(this);
locationListArray.setOnItemSelectedListener(this);
btnUpd = (Button) findViewById(R.id.btnLocUpdt);
btnUpd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
currselectedBus=res1.getText().toString();
currselectedLocat=res2.getText().toString();
new LocationUpdateClass().execute( currselectedBus, currselectedLocat);
}
});
}
protected void onStart(){
super.onStart();
backTask1 bt1 = new backTask1();
bt1.execute();
backTask2 bt2 = new backTask2();
bt2.execute();
}
private class backTask1 extends AsyncTask<Void, Void, Void>{
ArrayList<String> list;
protected void onPreExecute (){
super.onPreExecute();
list = new ArrayList<>();
}
protected Void doInBackground(Void...params){
InputStream is=null;
String result ="";
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://livetipsdotnet.000webhostapp.com/fetchspinner1.php");
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch (IOException e){
e.printStackTrace();
}
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
String line="";
while ((line=bufferedReader.readLine())!=null)
{
result +=line;
}
is.close();
}
catch (IOException e){
e.printStackTrace();
}
try {
JSONArray jsonArray = new JSONArray(result);
for (int i=0; i<jsonArray.length(); i++)
{
JSONObject jsonObject = jsonArray.getJSONObject(i);
list.add(jsonObject.getString("id"));
}
}catch (JSONException e){
e.printStackTrace();
}
return null;
}
protected void onPostExecute (Void result){
listitems1.addAll(list);
adapter1.notifyDataSetChanged();
progressbar.setVisibility(View.GONE);
}
}//end of first spinner class
private class backTask2 extends AsyncTask<Void, Void, Void>{
ArrayList<String> list;
protected void onPreExecute (){
super.onPreExecute();
list = new ArrayList<>();
}
protected Void doInBackground(Void...params){
InputStream is=null;
String result ="";
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://livetipsdotnet.000webhostapp.com/fetchspinner2.php");
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch (IOException e){
e.printStackTrace();
}
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
String line="";
while ((line=bufferedReader.readLine())!=null)
{
result +=line;
}
is.close();
}
catch (IOException e){
e.printStackTrace();
}
try {
JSONArray jsonArray = new JSONArray(result);
for (int i=0; i<jsonArray.length(); i++)
{
JSONObject jsonObject = jsonArray.getJSONObject(i);
list.add(jsonObject.getString("name"));
}
}catch (JSONException e){
e.printStackTrace();
}
return null;
}
protected void onPostExecute (Void result){
listitems2.addAll(list);
adapter2.notifyDataSetChanged();
progressbar.setVisibility(View.GONE);
}
}
public class LocationUpdateClass extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... params) {
String busID = params[0];
String busLoc = params[1];
String link = "http://livetipsdotnet.000webhostapp.com/buslocationupdt.php?busID="
+ busID + "&busLoc=" + busLoc;
try {
OkHttpClient client = new OkHttpClient();
RequestBody postData = new FormBody.Builder()
.add("type", "json")
.build();
Request request = new Request.Builder()
.url(link)
.post(postData)
.build();
Response response = client.newCall(request).execute();
String result = response.body().string();
return result;
} catch (Exception e) {
return e.getMessage().toString();
}
}
@Override
protected void onPostExecute(String getResult) {
super.onPostExecute(getResult);
DriversLocationUpdate.this.logstatus = getResult;
vtxtValidation.setText(getResult);
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View selectedItemView, int position, long id) {
switch (parent.getId()){
case R.id.bus_arrays:
String list1 = parent.getItemAtPosition(position).toString();
res1.setText(list1);
Toast.makeText(parent.getContext(), "OnItemSelectedListener : " + list1, Toast.LENGTH_SHORT).show();
break;
case R.id.location_arrays:
String list2 = parent.getItemAtPosition(position).toString();
res2.setText(list2);
Toast.makeText(parent.getContext(), "OnItemSelectedListener : " + list2, Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_drivers_location_update, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
return id == R.id.action_settings || super.onOptionsItemSelected(item);
}
}
公共类
DriversLocationUpdate扩展AppCompativeActivity实现AdapterView.OnItemSelectedListener{
ArrayList listitems1=新的ArrayList();
阵列适配器1;
ArrayList listitems2=新的ArrayList();
阵列适配器2;
微调器busListArray,locationListArray;
ProgressBar ProgressBar;
按钮btnUpd;
字符串currselectedBus、logstatus、currselectedLocat;
TextView vtxtValidation;
文本视图res1、res2;
//字符串[]arrayUserinfo;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u drivers\u location\u update);
progressbar=(progressbar)findViewById(R.id.loginprogress);
res1=(TextView)findViewById(R.id.spin1);
res2=(TextView)findViewById(R.id.spin2);
busListArray=(微调器)findViewById(R.id.bus_数组);
adapter1=新阵列适配器(此,R.layout.support\u simple\u spinner\u dropdown\u项,listitems1);
busListArray.setAdapter(适配器1);
locationListArray=(微调器)findViewById(R.id.location\u数组);
adapter2=新阵列适配器(此,R.layout.support\u simple\u spinner\u dropdown\u项,listitems2);
locationListArray.setAdapter(适配器2);
busListArray.setOnItemSelectedListener(此);
locationListArray.setOnItemSelectedListener(此);
btnUpd=(按钮)findViewById(R.id.btnLocUpdt);
btnUpd.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
currselectedBus=res1.getText().toString();
currselectedLocat=res2.getText().toString();
new LocationUpdateClass().execute(currselectedBus,currselectedLocat);
}
});
}
受保护的void onStart(){
super.onStart();
backTask1 bt1=新的backTask1();
bt1.execute();
backTask2 bt2=新的backTask2();
bt2.execute();
}
私有类backTask1扩展了AsyncTask{
数组列表;
受保护的void onPreExecute(){
super.onPreExecute();
列表=新的ArrayList();
}
受保护的Void doInBackground(Void…参数){
InputStream=null;
字符串结果=”;
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://livetipsdotnet.000webhostapp.com/fetchspinner1.php");
HttpResponse response=httpClient.execute(httpPost);
HttpEntity=response.getEntity();
is=entity.getContent();
}
捕获(IOE异常){
e、 printStackTrace();
}
试一试{
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(即“UTF-8”);
字符串行=”;
而((line=bufferedReader.readLine())!=null)
{
结果+=行;
}
is.close();
}
捕获(IOE异常){
e、 printStackTrace();
}
试一试{
JSONArray JSONArray=新JSONArray(结果);
对于(inti=0;i试试这个.)
final Spinner spinner = (Spinner)findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String items = spinner.getSelectedItem().toString();
//Here give a network call
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
final Spinner Spinner=(Spinner)findViewById(R.id.Spinner);
spinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView arg0、视图arg1、内部arg2、长arg3){
String items=spinner.getSelectedItem().toString();
//这里有一个网络电话
}
@凌驾
未选择公共无效(AdapterView arg0){
}
});
我的应用程序崩溃了
。这是一个猜测异常测验吗?
。请看。您好,也许您应该提供应用程序崩溃时得到的控制台错误日志,或者至少试着在运行时调试代码,看看它崩溃的原因。有了更具体的问题,任何人都可以更容易地帮助您谢谢,雨果你的建议救了我。从错误日志中,我可以解决问题。