Java 从android中的服务器获取数据
嗨,我写了一个android程序,从服务器上获取数据并在列表视图中显示 但当我运行程序时,它会显示空列表视图 请查看我的代码并帮助我: 主要活动类Java 从android中的服务器获取数据,java,android,listview,Java,Android,Listview,嗨,我写了一个android程序,从服务器上获取数据并在列表视图中显示 但当我运行程序时,它会显示空列表视图 请查看我的代码并帮助我: 主要活动类 package com.example.delta.mysite2; public class MainActivity extends ListActivity { String[] id,name,content,commentcount; String tid,tname,tcontent,tcomment; private int pag
package com.example.delta.mysite2;
public class MainActivity extends ListActivity {
String[] id,name,content,commentcount;
String tid,tname,tcontent,tcomment;
private int page=1;
private int count;
private String res="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new getpost().execute();
}
private void makearray(int c){
id=new String[c];
name=new String[c];
content=new String[c];
commentcount=new String[c];
Arrays.fill(name,"");
}
public class getpost extends AsyncTask{
@Override
protected Object doInBackground(Object[] params) {
try {
String data= URLEncoder.encode("page","utf8")+"="+URLEncoder.encode(page+"","utf8");
URL link=new URL("http://deltaspot.ir/test_android_server/wp/app/get.php");
URLConnection connect=link.openConnection();
//send data
connect.setDoOutput(true);
OutputStreamWriter wr=new OutputStreamWriter(connect.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader reader=new BufferedReader(new InputStreamReader(connect.getInputStream()));
StringBuilder sb=new StringBuilder();
String line=null;
while ((line=reader.readLine())!=null){
sb.append(line);
}
res=sb.toString();
for(int y=0;y<4;y++){
if(res.charAt(y)=='☻'){
count=Integer.parseInt(res.substring(0,y));
res=res.substring(y+1);
break;
}
}
makearray(count);
int f=0,c=0;
for(int i=0;i<res.length();i++){
if(res.charAt(i)=='☻'){
String temp=res.substring(f,i);
if(c==0){
tid=temp;
}
if(c==1){
tname=temp;
}
if(c==2){
tcontent=temp;
}
if(c==3){
tcomment=temp;
for(int t=0;t<count;t++){
if(name[t].equals("")){
id[t]=tid;
name[t]=tname;
content[t]=tcontent;
commentcount[t]=tcomment;
break;
}
}
c=-1;
}
f=i+1;
c+=1;
}
}
}catch (Exception e){
res=e.toString();
}
return "";
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
setListAdapter(new IA());
}
}
public class IA extends ArrayAdapter<String>{
public IA() {
super(MainActivity.this,R.layout.row_main);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater in=getLayoutInflater();
View row=in.inflate(R.layout.row_main, parent, false);
TextView titr=(TextView) row.findViewById(R.id.row_main_titr);
TextView matn=(TextView) row.findViewById(R.id.row_main_content);
TextView extra=(TextView) row.findViewById(R.id.row_main_extra);
ImageView img=(ImageView) row.findViewById(R.id.row_main_image);
titr.setText(name[position]);
matn.setText(content[position]);
extra.setText("comment"+commentcount[position]);
return (row);
}
}
package com.example.delta.mysite2;
公共类MainActivity扩展了ListActivity{
字符串[]id、名称、内容、commentcount;
字符串tid、T名称、T内容、T注释;
私有int页=1;
私人整数计数;
私有字符串res=“”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
新建getpost().execute();
}
私有void生成数组(int c){
id=新字符串[c];
name=新字符串[c];
内容=新字符串[c];
commentcount=新字符串[c];
数组。填充(名称为“”);
}
公共类getpost扩展了异步任务{
@凌驾
受保护对象doInBackground(对象[]参数){
试一试{
字符串数据=urlcoder.encode(“页面”,“utf8”)+“=”+urlcoder.encode(页面+”,“utf8”);
URL链接=新URL(“http://deltaspot.ir/test_android_server/wp/app/get.php");
URLConnection=link.openConnection();
//发送数据
connect.setDoOutput(true);
OutputStreamWriter wr=新的OutputStreamWriter(connect.getOutputStream());
wr.写入(数据);
wr.flush();
BufferedReader=new BufferedReader(new InputStreamReader(connect.getInputStream());
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
某人附加(行);
}
res=sb.toString();
对于(int y=0;y,我认为您的问题可能是adpater(IA)让getView方法完成了我认为应该由bindView方法完成的事情。也就是说,尝试将getView更改为bindView
此外,我相信充气机代码:-
LayoutInflater in=getLayoutInflater();
View row=in.inflate(R.layout.row_main, parent, false);
属于newView方法
下面是一个工作适配器的示例。但是,这是一个光标。我相信它们非常相似:-
public class Database_Inspector_AislesDB_Adapter extends CursorAdapter {
public Database_Inspector_AislesDB_Adapter(Context context, Cursor cursor, int flags) {
super(context, cursor, 0);
}
@Override
public View getView(int position, View convertview, ViewGroup parent) {
View view = super.getView(position, convertview, parent);
Context context = view.getContext();
if(position % 2 == 0) {
view.setBackgroundColor(ContextCompat.getColor(context, R.color.colorlistviewroweven));
} else {
view.setBackgroundColor(ContextCompat.getColor(context, R.color.colorlistviewrowodd));
}
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView textviewaisleid = (TextView) view.findViewById(R.id.adiae_aislesdb_id);
TextView textviewaisleshopref = (TextView) view.findViewById(R.id.adiae_aislesdb_shopref);
TextView textviewaislesorder = (TextView) view.findViewById(R.id.adiae_aislesdb_order);
TextView textviewaislesaislename = (TextView) view.findViewById(R.id.adiae_aislesdb_aislename);
textviewaisleid.setText(cursor.getString(ShopperDBHelper.AISLES_COLUMN_ID_INDEX));
textviewaislesaislename.setText(cursor.getString(ShopperDBHelper.AISLES_COLUMN_NAME_INDEX));
textviewaislesorder.setText(cursor.getString(ShopperDBHelper.AISLES_COLUMN_ORDER_INDEX));
textviewaisleshopref.setText(cursor.getString(ShopperDBHelper.AISLES_COLUMN_SHOP_INDEX));
}
public View newView(Context context, Cursor cursos, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.activity_database_inspect_aislesdb_entry, parent, false);
}
}
本例中的getView正在交替背景色。告诉适配器x行将在那里
替换这个
super(MainActivity.this,R.layout.row_main);
带(通过添加计数)
您可以检查您的res
变量(res=sb.toString();
)是否为空或null吗?(如果无法调试,只需放入Toast.makeText(MainActivity.this,res,Toast.LENGTH\u SHORT).show();
)你能给我们看更多你的日志吗?res变量是emptyi把getview改成bindview,但它不是固定的。用这段代码我想给我看这个链接的数据:但是我不能这样做,你能给我一个来源帮我做这件事吗,谢谢。我建议把任务分成两部分o块。1)获取数据,构建数组并打印数组,以检查数组是否已填充;2)使用手动填充了几个条目的数组,使listview适配器等正常工作。当您对两者都满意时,将它们合并在一起。我改为超级(MainActivity.this,R.layout.row_main,count);但它还不起作用。通过记录检查计数,它是否在调用super之前给出正值。
super(MainActivity.this,R.layout.row_main);
super(MainActivity.this,R.layout.row_main, count);