0

另一个Android初学者在这里!

好的,我在使用 Toast 帮助我检查错误时感到沮丧。我收到 403 禁止错误,我不知道是什么原因造成的。url 是正确的,因为我的另一个函数从数据库返回数据。

public class FillKeg extends Activity {

    Spinner spinBeerList;
    ArrayList<String> BeerList;
    ArrayAdapter<String> adapter;

    JSONArray jArray;
    String result = null;
    InputStream is = null;
    StringBuilder sb = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fillkeg);
        spinBeerList = (Spinner)findViewById(R.id.spinBeerList);
        BeerList = new ArrayList<String>();
        new getBeerList().execute("http://*****/kegdroid/getBeerList.php");
        adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, BeerList);    
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinBeerList.setAdapter(adapter);
    }

    private class getBeerList extends AsyncTask<String, Void, String> {

        protected String doInBackground(String... urls) {
            //http post
            for (String url : urls) {
                DefaultHttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);
                try{
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity entity = response.getEntity();
                    is = entity.getContent();

                    BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
                    sb = new StringBuilder();
                    sb.append(reader.readLine() + "\n");

                    String line = "0";
                    while ((line = reader.readLine()) != null) {
                            sb.append(line + "\n");
                    }
                    is.close();
                    result = sb.toString();
                }
                catch(Exception e){
                    Log.e("log_tag", "Error in http connection" + e.toString());
                }
            }
            return result;
        }

        protected void onPostExecute(String result){
            try{
                jArray = new JSONArray(result);
                JSONObject json_data = null;
                for(int i = 0; i < jArray.length(); i++){
                    json_data = jArray.getJSONObject(i);
                    BeerList.add(json_data.getString("ContentsName"));
                }    
            }
            catch(JSONException ex){
                ex.printStackTrace();
            } 
            catch (ParseException ex) {
                ex.printStackTrace();
            }
        }

    }
}
4

2 回答 2

0

您无需调用即可更新您的ArrayList支持。尝试在.AdapternotifyDataSetChangedadapter.notifyDataSetChanged();onPostExecute

或者,您可以调用adapter.add(json_data.getString("ContentsName"));来代替类似的调用,BeerList并且Spinner应该自动更新。

于 2012-04-10T23:56:21.573 回答
0

我对此感到非常愚蠢,但我的错误源于 Apache 配置而不是应用程序代码。我的 WAMP 安装的最新 Apache 更新拒绝了对我的网络 IP 的调用访问。

非常感谢 OcuS 和 Brian Cooley 帮助解决我的代码的其他方面!

于 2012-04-11T03:06:04.140 回答