高手指教一下
高手指教一下
zhouxuebao 发表于4年前
高手指教一下
  • 发表于 4年前
  • 阅读 7
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

 

public class HttpUtils {

 /**
  *
  * @param path
  * @param encoding
  * @return
  */
 public static String sendPostMethod(String path, String encoding) {
  String result = "";
  HttpClient httpClient = new DefaultHttpClient();
  try {
   HttpPost post = new HttpPost(path);
   HttpResponse response = httpClient.execute(post);
   if (response.getStatusLine().getStatusCode() == 200) {
    result = EntityUtils.toString(response.getEntity(), encoding);
   }
  } catch (Exception e) {
   // TODO: handle exception
   e.printStackTrace();
  } finally {
   httpClient.getConnectionManager().shutdown();
  }
  return result;
 }
}

 

 

public class JsonTools {
  public  static List<String> parseJsonList(String jsonString){
   List<String> list=new ArrayList<String>();
   try {
   JSONObject jsonObject=new JSONObject(jsonString);
   JSONArray jsonArray=jsonObject.getJSONArray("citys");
   for (int i = 0; i < jsonArray.length(); i++) {
    list.add(jsonArray.getString(i));
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  
  
  return null;
  
  }
}

 

 

 

public class MainActivity extends Activity {
 
    private ListView listView;
    private ArrayAdapter<String> adapter;
    private ProgressDialog dialog;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        dialog=new ProgressDialog(this);
        dialog.setTitle("XX");
        dialog.setMessage("load...");
        listView=(ListView) findViewById(R.id.listView1);
        new MyTask().execute("http://192.168.1.2:8090/productweb/CityAction?type=json");
        /**
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
      //  adapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, getDataSource());
       // adapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_multiple_choice, getDataSource());
        adapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_single_choice, getDataSource());
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> parent, View view,
     int position, long id) {
     Toast.makeText(MainActivity.this, "---->>"+adapter.getItem(position), 3000).show();
   //  Toast.makeText(MainActivity.this, "---->>"+position, 3000).show();
    
   }
         
  });**/
      
        }
        /*   public ArrayList<String> getDataSource(){
            ArrayList<String> list=new ArrayList<String>();
            for (int i = 0; i <30; i++) {
     list.add("zhouxuebao"+i);
   }
            return list;
           }*/
     public class MyTask extends AsyncTask<String,Void,List<String>>{
             
     
      
   @Override
  protected void onPreExecute() {
   super.onPreExecute();
   dialog.show();
  }

   @Override
   protected List<String> doInBackground(String... params) {
    String jsonString=HttpUtils.sendPostMethod(params[0], "utf-8");
    List<String> list=JsonTools.parseJsonList(jsonString);
    return list;
   }

   @Override
   protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
   }

   @Override
   protected void onPostExecute(List<String> result) {
    super.onPostExecute(result);
    adapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, result);
    System.out.println("zhou");//这一行可以在控制台显示
    listView.setAdapter(adapter);// 断点这一行跳不过去
    System.out.println("xue");//控制台不打印
    adapter.notifyDataSetChanged();//
    dialog.dismiss();
   }
     
     }
   
   
}

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

 
     <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>

 

清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.laoluo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="14" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

共有 人打赏支持
粉丝 0
博文 1
码字总数 426
×
zhouxuebao
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: