另一個Android初學者在這裏!無法從遠程數據庫填充微調器
好的,我在使用吐司幫我檢查錯誤時進行了仿製。我收到了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();
}
}
}
}
NO。決不。 EVER。在UI線程上做HTTP消息。這個。 IS。壞。 – OcuS 2012-04-10 19:06:17
好吧,我改變它來實現AsyncTask。然而,微調仍然沒有人口。 – Crevax 2012-04-10 23:53:11