1

首先,我是新手,我已经按照教程使用 Yii 框架作为服务器创建网站。一个带有 CRUD 功能的简单网站已经构建完成。现在我想将它与我的 android 应用程序连接起来。

我的平台:Win 7 64、Eclipse IDE、Android ADT、GenyMotion。

/* Yii 侧 */

这是我在 (protected\controllers\EmployeeController.php) 上的 EmployeeController

public function actionGetEmployee($id)
{
    header('Content-type: application/json');
    $employee = Employee::model()->findAll();
    echo CJSON::encode($employee);
    Yii::app()->end();
}

上面代码的结果是这样的:

[
{
    "id": "1",
    "departmentId": "1",
    "firstName": "Hendy",
    "lastName": "Nugraha",
    "gender": "female",
    "birth_date": "1987-03-16",
    "marital_status": "Single",
    "phone": "856439112",
    "address": "Tiban Mutiara View ",
    "email": "hendy.nugraha87@yahoo.co.id",
    "ext": "1",
    "hireDate": "2012-06-30 00:00:00",
    "leaveDate": "0000-00-00 00:00:00"
},
{
    "id": "2",
    "departmentId": "2",
    "firstName": "Jay",
    "lastName": "Branham",
    "gender": "male",
    "birth_date": "0000-00-00",
    "marital_status": "Single",
    "phone": "0",
    "address": "",
    "email": "jaymbrnhm@labtech.org",
    "ext": "2",
    "hireDate": "0000-00-00 00:00:00",
    "leaveDate": "0000-00-00 00:00:00"
}
]

/* 现在在 Android 端 */

这是 main.xml :

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/header"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Daftar Biodata Karyawan Online" />

        <TableLayout
            android:id="@+id/tableLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TableRow
                android:id="@+id/tableRow1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" >

                <Button
                    android:id="@+id/btnrefresh"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Refresh" />

                <Button
                    android:id="@+id/btnedit"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Edit" />

                <Button
                    android:id="@+id/btnhapus"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Hapus" />
            </TableRow>
        </TableLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/detail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TableLayout
            android:id="@+id/tableLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1" >
        </TableLayout>
    </LinearLayout>
</LinearLayout>
 </ScrollView>

这是我的 Akses_Server_Activity :

    public class Akses_Server_Activity extends Activity{

static String url = "http://10.0.2.2/restayii/index.php/department/getdepartment?id";
static final String Employee_ID = "id";
static final String Employee_Dept_ID = "departmentId";
static final String Employee_First_Name = "firstName";
static final String Employee_Last_Name = "lastName";
static final String Employee_Gender = "gender";
static final String Employee_Birth_Date = "birth_date";
static final String Employee_Marital_Status = "marital_status";
static final String Employee_Phone_Number = "phone";
static final String Employee_Address = "address";
static final String Employee_Email = "email";
static final String Employee_Ext = "ext";
static final String Employee_Hire_Date = "hireDate";
static final String Employee_Leave_Date = "leaveDate";
JSONArray employee = null;
JSONObject json_object;
Button btnrefresh;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.daftar_employee_online);

    btnrefresh = (Button) findViewById(R.id.btnrefresh);
    btnrefresh.setOnClickListener(new View.OnClickListener() {  
        @Override  
        public void onClick(View v) {
            try {
                Toast.makeText(getApplicationContext(), "Hendy", Toast.LENGTH_LONG).show();
                new MyAsynTask().execute();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }  
    });
}

public class JSONParser {

    InputStream is = null;
    JSONObject jObj = null;
    String json = "";

    public JSONObject GetJson(String url) {

        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        return jObj;

    }
}

public class MyAsynTask extends AsyncTask<String, Void, JSONObject>{

    @Override
    protected JSONObject doInBackground(String... arg0) {

        JSONParser json_parse = new JSONParser();
        json_object = json_parse.GetJson(url);
        return null;
    }

    protected void onPostExecute(String Result){
        // com
        try {
            employee = json_object.getJSONArray("employee");
            TableLayout table_layout =(TableLayout) findViewById(R.id.tableLayout);
            table_layout.removeAllViews();
            int jml_baris = employee.length();
            String [][] data_employee = new String [jml_baris][13];
            for(int i=0;i<jml_baris;i++){
                JSONObject jobj = employee.getJSONObject(i);
                data_employee[i][0] = jobj.getString(Employee_ID);
                data_employee[i][1] = jobj.getString(Employee_Dept_ID);
                data_employee[i][2] = jobj.getString(Employee_First_Name);
                data_employee[i][3] = jobj.getString(Employee_Last_Name);
                data_employee[i][4] = jobj.getString(Employee_Gender);
                data_employee[i][5] = jobj.getString(Employee_Birth_Date);
                data_employee[i][6] = jobj.getString(Employee_Marital_Status);
                data_employee[i][7] = jobj.getString(Employee_Phone_Number);
                data_employee[i][8] = jobj.getString(Employee_Address);
                data_employee[i][9] = jobj.getString(Employee_Email);
                data_employee[i][10] = jobj.getString(Employee_Ext);
                data_employee[i][11] = jobj.getString(Employee_Hire_Date);
                data_employee[i][12] = jobj.getString(Employee_Leave_Date);
            }

            TableLayout.LayoutParams ParameterTableLayout = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
            for(int j=0; j<jml_baris; j++){  
                TableRow  table_row = new TableRow(null);  
                table_row.setBackgroundColor(Color.BLACK);  
                table_row.setLayoutParams(ParameterTableLayout);  
                TableRow.LayoutParams ParameterTableRow = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);  
                ParameterTableRow.setMargins(1,1,1,1);
                for(int kolom = 0; kolom < 13; kolom++){
                    TextView TV= new TextView(null);
                    TV.setText(data_employee[j][kolom]);  
                    TV.setTextColor(Color.BLACK);  
                    TV.setPadding(1, 4, 1, 4);  
                    TV.setGravity(Gravity.LEFT);  
                    TV.setBackgroundColor(Color.BLUE);  
                    table_row.addView(TV,ParameterTableRow);  
                }   
                table_layout.addView(table_row); 
            }

        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}
    }

这是日志猫:

11-04 16:23:47.273: E/JSON Parser(1867): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject

我的问题是:当 android 应用程序运行时,我单击刷新按钮,它没有显示包含员工 json 数据的表格布局。任何帮助都会非常感激。谢谢。

4

1 回答 1

0

我认为您在从 Android 应用程序发出请求时出错,因为<!DOCTYPE是网页的开始。尝试查看来自 Android 应用程序的请求结果。

于 2013-11-04T10:15:36.950 回答