从web api获取值到android spinner

时间:2014-08-05 07:53:42

标签: android rest web

这里是我从web api获取值的代码。在我的代码中没有错误。但在模拟器中没有显示值。请告诉我我在哪里犯了错误。让我知道正确的代码是什么。

public class MainActivity extends ActionBarActivity {
Spinner sp1;

String url1=" http://www.cartrade.com/testmobileapplication/GetTopCitiesList.php?     app_id=<random number>&action=Get Cities";

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

    Spinner sp1=(Spinner)findViewById(R.id.spinner1);
}




public class Mysync extends AsyncTask<List<String>, List<String>, List<String>>{

    @Override
    protected List<String> doInBackground(List<String>... arg0) {
        // TODO Auto-generated method stub
        HttpClient httpclient = new DefaultHttpClient();
        // Prepare a request object
        HttpGet httpget = new HttpGet(url1); 
        // Execute the request
        HttpResponse response;

        JSONArray arr = new JSONArray();
        try {
           response = httpclient.execute(httpget);

           HttpEntity entity = response.getEntity();

           if (entity != null && response.getStatusLine().getStatusCode() == 200) {
                    // A Simple JSON Response Read
                    InputStream instream = entity.getContent();
                    String result = convertStreamToString(instream);
                    arr=new JSONArray(result);
                    instream.close();

                }
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
               // Log.e(TAG,e.toString());
            } catch (IOException e) {
                // TODO Auto-generated catch block
               // Log.e(TAG,e.toString());
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                //Log.e(TAG,e.toString());
            }
        List<String> list = new ArrayList<String>();
        for(int i = 0; i < arr.length(); i++){
            try {
                list.add(arr.getJSONObject(i).getString("name"));
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
            return list;
    }
    protected void onPostExecute(List<String> result){
         sp1=(Spinner)findViewById(R.id.spinner1);
        ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(), R.layout.support_simple_spinner_dropdown_item);
        sp1.setAdapter(adapter);

    }

    }


public String convertStreamToString(InputStream is) {
    // TODO Auto-generated method stub
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
       // Log.e(TAG + "ERROR",e.toString());

    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
           // Log.e(TAG + "ERRO",e.toString());
        }
    }
    return sb.toString();
}

}

1 个答案:

答案 0 :(得分:0)

行下定义列表

字符串url1 =&#34; http://www.cartrade.com/testmobileapplication/GetTopCitiesList.php? app_id =&amp; action =获取城市&#34;;

喜欢:List<String> list ;

并尝试更改行

  

List list = new ArrayList();

通过

  

list = new ArrayList();

和更改

ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(), R.layout.support_simple_spinner_dropdown_item);

ArrayAdapter<String> adapter=new ArrayAdapter<String>(getApplicationContext(), R.layout.support_simple_spinner_dropdown_item,result);

希望这可以帮到你!