从数据库dosn工作填充微调器

时间:2016-04-23 23:53:54

标签: java android android-studio

我正在尝试从darabase填充微调器,但应用程序崩溃了,#34;不幸的是,"应用程序"已停止了。"在logcat中我得到了这个错误"

在com.exemple.user.modele.Categorie.onCreate(Categorie.java:43)"

当我删除所有try catch代码时,活动有效,但是我从同一个应用程序中的另一个活动复制了这个代码来填充listView并且它有效,所以如果有人可以帮助我,谢谢!

package com.exemple.user.modele;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

public class Categorie extends AppCompatActivity {

Spinner spinner1;
String result = null;
String line = null;
InputStream is = null;
String cat_url = "http://10.0.2.2/webapp/categories_spinner.php";
ArrayList<String> list1=new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_categorie);

    spinner1 = (Spinner) findViewById(R.id.sSelect);

    try {
        URL url=new URL(cat_url);
        HttpURLConnection con= (HttpURLConnection) url.openConnection();
        is=new BufferedInputStream(con.getInputStream());

        BufferedReader br=new BufferedReader(new InputStreamReader(is));

        StringBuffer sb=new StringBuffer();

        if(br != null) {

            while ((line=br.readLine()) != null) {
                sb.append(line+"\n");
            }

        }

        result = sb.toString();

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        if(is != null)
        {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    try {
        //ADD THAT DATA TO JSON ARRAY FIRST
        JSONArray ja = new JSONArray(result);

        //CREATE JO OBJ TO HOLD A SINGLE ITEM
        JSONObject jo = null;

        list1.clear();

        //LOOP THRU ARRAY
        for (int i = 0; i < ja.length(); i++) {
            jo = ja.getJSONObject(i);

            //RETRIOEVE NAME
            String name = jo.getString("name");

            //ADD IT TO OUR ARRAYLIST
            list1.add(name);
        }
        spinner_fn();

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

private void spinner_fn() {
    ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(Categorie.this,
            android.R.layout.simple_spinner_item, list1);
    spinner1.setAdapter(dataAdapter1);

    spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            spinner1.setSelection(position);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });
}

}

0 个答案:

没有答案