喜从服务器的数据我写的android程序从服务器获取数据,并显示在列表视图 但是当我运行程序它告诉我空列表视图 请参阅我的代码,并帮助我:得到的Android
MainActiviy类
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);
}
}
}
和布局代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="@+id/row_main_image" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/row_main_titr"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/row_main_content"
android:layout_below="@+id/row_main_titr"
android:layout_alignLeft="@+id/row_main_titr"
android:layout_alignStart="@+id/row_main_titr"
android:layout_marginTop="34dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/row_main_extra"
android:layout_below="@+id/row_main_image"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
这是我的代码,请帮我解决我的问题。 谢谢。
可以检查你的'res'变量('RES = sb.toString();')不为空或空值? (如果你不能调试只是放一个'Toast.makeText(MainActivity.this,res,Toast.LENGTH_SHORT).show();') – pleft
你能告诉我们更多你的日志吗? – ghassen92
res变量为空 – alireza