我正在尝试将 String [] json 转换为外部服务器上的查询并显示在列表视图中
public class ConectPhp extends ListActivity {
public static InputStream inpString = null;
String result = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<NameValuePair> parametros = new ArrayList<NameValuePair>();
parametros.add(new BasicNameValuePair("DATA", "2011-11-05"));
try{
HttpClient httpcliente = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.254.254/mobile/ConexaoWcrm.php/");
httppost.setEntity(new UrlEncodedFormEntity(parametros));
HttpResponse response = httpcliente.execute(httppost);
HttpEntity entity = response.getEntity();
inpString = entity.getContent();
} catch (Exception e){
Log.e("log_tag", "Error to conect a "+e.toString());
}
//将返回值转换为字符串
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(inpString, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = reader.readLine()) != null){
sb.append(line+"\n");
}
inpString.close();
result=sb.toString();
}catch (Exception e){
Log.e("log_tag","Error to convert result"+e.toString());
}
//这里会把数据传给json,但是不知道怎么做,然后存入listview。
try{
JSONArray jArray = new JSONArray(result);
String[] clientes = new String[jArray.length()];
for(int i=0; i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
clientes[i]=json_data.getString("clientnome");
}
this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, clientes));
}catch(Exception e){
Log.e("log_tag","Error popular data "+e.toString());
}
我是新手,已经有了这个困难的项目,如果他们有任何提示给我也将被接受,必须列出某些数据库用于外部列表视图。