如何使用此java代码连接到我的Android站点?

时间:2016-05-22 08:32:02

标签: java php android xml android-studio

我在android中创建程序以从我的本地站点获取数据并显示数据 列表视图,但我在程序中有一些问题,我无法显示数据 在列表视图中

这是我的代码:

这是android studio中连接到我的列表视图和我的本地站点的代码:

import CommonCrypto

我的列表视图:

#import

这是我自定义列表视图的代码:

package com.example.delta.social_food;

import android.app.ListActivity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.lang.reflect.Array;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Arrays;

/**
 * Created by delta on 5/8/2016.
 */
public class Shared_food_list extends ListActivity {

String[] username,description;
String tusername,tdescription;
private int page=1;
private int count;

private String res="";

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

    new getpost().execute();


}

private void makearray(int c){
    username=new String[c];
    description=new String[c];

    Arrays.fill(username,"");
}

public class getpost extends AsyncTask{

    @Override
    protected Object doInBackground(Object[] params) {

        try {
            String data= URLEncoder.encode("txt_page", "UTF8")+"="+URLEncoder.encode(page+"","UTF8");
            URL link=new URL("http://192.168.1.4/social_food/main/show_food_list");
            URLConnection connect=link.openConnection();

            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){
                        tusername=temp;
                    }
                    if(c==1){
                        tdescription=temp;

                        for(int t=0;t<count;t++){
                            if(username[t].equals("")){
                                username[t]=tusername;
                                description[t]=tdescription;
                                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(Shared_food_list.this, R.layout.show_list,username);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater in=getLayoutInflater();
        View row=in.inflate(R.layout.show_list, parent, false);

        TextView users=(TextView)findViewById(R.id.show_list_username);
        TextView desc=(TextView)findViewById(R.id.show_list_message);

        users.setText(username[position]);
        desc.setText(description[position]);

        return (row);
    }
}

}

这是我的php站点代码,带有codeigniter:

控制器代码:

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

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal" />
</LinearLayout>

型号代码:

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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="User Name"
    android:id="@+id/show_list_username" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Message"
    android:id="@+id/show_list_message"
    android:layout_gravity="right" />

和我的观看代码:

public function show_food_list(){
$page = $this -> input -> post('txt_page');
if($page=="1"){
$this -> load -> model('Database','model');
$data['infos'] = $this -> model -> get_food_list();
}
$this -> load -> view('food',$data);
}

1 个答案:

答案 0 :(得分:0)

来自stackoverflow link 如果您在Android工作室模拟器上运行应用程序,请使用IP地址http://10.0.2.1http://10.0.2.2。 否则,如果在genymotion上运行,请使用http://10.0.3.2