Home > php教程 > php手册 > android+json+php+mysql实现用户反馈功能

android+json+php+mysql实现用户反馈功能

WBOY
Release: 2016-07-11 20:00:37
Original
1033 people have browsed it

相信每个项目都会有用户反馈建议等功能,这个实现的方法很多,下面是我实现的方法,供大家交流。首先看具体界面,三个字段。名字,邮箱为选填,可以为空,建议不能为空。如有需要可以给我留言。

下面贴出布局代码,这里用到一个就是把另外一个布局文件引入到这个布局中。

<span style="color: #0000ff;"></span><span style="color: #ff00ff;">xml version="1.0" encoding="utf-8"</span><span style="color: #0000ff;">?></span>
<span style="color: #0000ff;"><span style="color: #800000;">LinearLayout </span><span style="color: #ff0000;">xmlns:android</span><span style="color: #0000ff;">="http://schemas.android.com/apk/res/android"</span><span style="color: #ff0000;">
    android:layout_width</span><span style="color: #0000ff;">="match_parent"</span><span style="color: #ff0000;">
    android:layout_height</span><span style="color: #0000ff;">="match_parent"</span><span style="color: #ff0000;">
    android:orientation</span><span style="color: #0000ff;">="vertical"</span><span style="color: #ff0000;">
    android:background</span><span style="color: #0000ff;">="@color/bg_gray"</span> <span style="color: #0000ff;">></span>
    <span style="color: #0000ff;"><span style="color: #800000;">include </span><span style="color: #ff0000;">layout</span><span style="color: #0000ff;">="@layout/uphead"</span><span style="color: #0000ff;">/></span>
    
    <span style="color: #008000;"><!--</span><span style="color: #008000;"> Name Label </span><span style="color: #008000;">--></span>
    <span style="color: #0000ff;"><span style="color: #800000;">TextView </span><span style="color: #ff0000;">android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;">
        android:layout_height</span><span style="color: #0000ff;">="wrap_content"</span><span style="color: #ff0000;">
        android:text</span><span style="color: #0000ff;">="名字(选填)"</span><span style="color: #ff0000;">
        android:paddingLeft</span><span style="color: #0000ff;">="10dip"</span><span style="color: #ff0000;">
        android:paddingRight</span><span style="color: #0000ff;">="10dip"</span><span style="color: #ff0000;">
        android:textColor</span><span style="color: #0000ff;">="@color/coffee"</span><span style="color: #ff0000;">
        android:paddingTop</span><span style="color: #0000ff;">="10dip"</span><span style="color: #ff0000;">
        android:textSize</span><span style="color: #0000ff;">="12sp"</span><span style="color: #0000ff;">/></span>
    
    <span style="color: #008000;"><!--</span><span style="color: #008000;"> Input Name </span><span style="color: #008000;">--></span>
    <span style="color: #0000ff;"><span style="color: #800000;">EditText </span><span style="color: #ff0000;">android:id</span><span style="color: #0000ff;">="@+id/inputName"</span><span style="color: #ff0000;">
        android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;">
        android:layout_height</span><span style="color: #0000ff;">="wrap_content"</span><span style="color: #ff0000;">
        android:layout_margin</span><span style="color: #0000ff;">="5dip"</span><span style="color: #ff0000;">
        android:layout_marginBottom</span><span style="color: #0000ff;">="15dip"</span><span style="color: #ff0000;">
        android:singleLine</span><span style="color: #0000ff;">="true"</span><span style="color: #0000ff;">/></span>
    
    <span style="color: #008000;"><!--</span><span style="color: #008000;"> Price Label </span><span style="color: #008000;">--></span>
    <span style="color: #0000ff;"><span style="color: #800000;">TextView </span><span style="color: #ff0000;">android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;">
        android:layout_height</span><span style="color: #0000ff;">="wrap_content"</span><span style="color: #ff0000;">
        android:text</span><span style="color: #0000ff;">="邮箱(选填)"</span><span style="color: #ff0000;">
        android:paddingLeft</span><span style="color: #0000ff;">="10dip"</span><span style="color: #ff0000;">
        android:paddingRight</span><span style="color: #0000ff;">="10dip"</span><span style="color: #ff0000;">
        android:textColor</span><span style="color: #0000ff;">="@color/coffee"</span><span style="color: #ff0000;">
        android:paddingTop</span><span style="color: #0000ff;">="10dip"</span><span style="color: #ff0000;">
        android:textSize</span><span style="color: #0000ff;">="12sp"</span><span style="color: #0000ff;">/></span>
    
    <span style="color: #008000;"><!--</span><span style="color: #008000;"> Input Price </span><span style="color: #008000;">--></span>
    <span style="color: #0000ff;"><span style="color: #800000;">EditText </span><span style="color: #ff0000;">android:id</span><span style="color: #0000ff;">="@+id/inputEmail"</span><span style="color: #ff0000;"> 
        android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;">
        android:layout_height</span><span style="color: #0000ff;">="wrap_content"</span><span style="color: #ff0000;">
        android:layout_margin</span><span style="color: #0000ff;">="5dip"</span><span style="color: #ff0000;">
        android:layout_marginBottom</span><span style="color: #0000ff;">="15dip"</span><span style="color: #ff0000;">
        android:singleLine</span><span style="color: #0000ff;">="true"</span><span style="color: #0000ff;">/></span>
    
    <span style="color: #008000;"><!--</span><span style="color: #008000;"> Description Label </span><span style="color: #008000;">--></span>
    <span style="color: #0000ff;"><span style="color: #800000;">TextView </span><span style="color: #ff0000;">android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;">
        android:layout_height</span><span style="color: #0000ff;">="wrap_content"</span><span style="color: #ff0000;">
        android:text</span><span style="color: #0000ff;">="建议(必填)"</span><span style="color: #ff0000;">
        android:paddingLeft</span><span style="color: #0000ff;">="10dip"</span><span style="color: #ff0000;">
        android:paddingRight</span><span style="color: #0000ff;">="10dip"</span><span style="color: #ff0000;">
        android:paddingTop</span><span style="color: #0000ff;">="10dip"</span><span style="color: #ff0000;">
        android:textColor</span><span style="color: #0000ff;">="@color/coffee"</span><span style="color: #ff0000;">
        android:textSize</span><span style="color: #0000ff;">="12sp"</span><span style="color: #0000ff;">/></span>
    
    <span style="color: #008000;"><!--</span><span style="color: #008000;"> Input description </span><span style="color: #008000;">--></span>
    <span style="color: #0000ff;"><span style="color: #800000;">EditText </span><span style="color: #ff0000;">android:id</span><span style="color: #0000ff;">="@+id/inputDesc"</span><span style="color: #ff0000;"> 
        android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;">
        android:layout_height</span><span style="color: #0000ff;">="wrap_content"</span><span style="color: #ff0000;">
        android:layout_margin</span><span style="color: #0000ff;">="5dip"</span><span style="color: #ff0000;">
        android:layout_marginBottom</span><span style="color: #0000ff;">="15dip"</span><span style="color: #ff0000;">
        android:lines</span><span style="color: #0000ff;">="4"</span><span style="color: #ff0000;">
        android:gravity</span><span style="color: #0000ff;">="top"</span><span style="color: #0000ff;">/></span>
    
    <span style="color: #008000;"><!--</span><span style="color: #008000;"> Button Create Product </span><span style="color: #008000;">--></span>
    <span style="color: #0000ff;"><span style="color: #800000;">Button </span><span style="color: #ff0000;">android:id</span><span style="color: #0000ff;">="@+id/btnCreateProduct"</span><span style="color: #ff0000;"> 
        android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;">
        android:layout_height</span><span style="color: #0000ff;">="wrap_content"</span><span style="color: #ff0000;">
        android:text</span><span style="color: #0000ff;">="提交"</span><span style="color: #ff0000;">
        android:textSize</span><span style="color: #0000ff;">="20sp"</span><span style="color: #ff0000;">
        android:textColor</span><span style="color: #0000ff;">="@color/coffee"</span>
        <span style="color: #0000ff;">/></span>
    
<span style="color: #0000ff;"></span><span style="color: #800000;">LinearLayout</span><span style="color: #0000ff;">></span></span></span></span></span></span></span></span></span></span>
Copy after login

下面贴出uphead的布局代码,里面用到一个TextView,一个Button为返回按钮。

<span style="color: #0000ff;"></span><span style="color: #ff00ff;">xml version="1.0" encoding="utf-8"</span><span style="color: #0000ff;">?></span>
<span style="color: #0000ff;"><span style="color: #800000;">RelativeLayout </span><span style="color: #ff0000;">xmlns:android</span><span style="color: #0000ff;">="http://schemas.android.com/apk/res/android"</span><span style="color: #ff0000;">
        android:layout_width</span><span style="color: #0000ff;">="fill_parent"</span><span style="color: #ff0000;">
        android:layout_height</span><span style="color: #0000ff;">="50dp"</span><span style="color: #ff0000;">
        android:background</span><span style="color: #0000ff;">="@drawable/top"</span> <span style="color: #0000ff;">></span>
        <span style="color: #0000ff;"><span style="color: #800000;">TextView
            </span><span style="color: #ff0000;">android:id</span><span style="color: #0000ff;">="@+id/tv_head"</span><span style="color: #ff0000;">
            android:layout_width</span><span style="color: #0000ff;">="wrap_content"</span><span style="color: #ff0000;">
            android:layout_height</span><span style="color: #0000ff;">="wrap_content"</span><span style="color: #ff0000;">
            android:layout_centerInParent</span><span style="color: #0000ff;">="true"</span><span style="color: #ff0000;">
            android:shadowColor</span><span style="color: #0000ff;">="#ff000000"</span><span style="color: #ff0000;">
            android:shadowDx</span><span style="color: #0000ff;">="2"</span><span style="color: #ff0000;">
            android:shadowDy</span><span style="color: #0000ff;">="0"</span><span style="color: #ff0000;">
            android:shadowRadius</span><span style="color: #0000ff;">="1"</span><span style="color: #ff0000;">
            android:text</span><span style="color: #0000ff;">=""</span><span style="color: #ff0000;">
            android:textColor</span><span style="color: #0000ff;">="@color/white"</span><span style="color: #ff0000;">
            android:textSize</span><span style="color: #0000ff;">="18sp"</span><span style="color: #ff0000;">
            android:textStyle</span><span style="color: #0000ff;">="bold"</span> <span style="color: #0000ff;">/></span>
        <span style="color: #0000ff;"><span style="color: #800000;">Button
            </span><span style="color: #ff0000;">android:id</span><span style="color: #0000ff;">="@+id/upback"</span><span style="color: #ff0000;">
            android:layout_width</span><span style="color: #0000ff;">="wrap_content"</span><span style="color: #ff0000;">
            android:layout_height</span><span style="color: #0000ff;">="wrap_content"</span><span style="color: #ff0000;">
            android:layout_alignParentLeft</span><span style="color: #0000ff;">="true"</span><span style="color: #ff0000;">
            android:layout_centerVertical</span><span style="color: #0000ff;">="true"</span><span style="color: #ff0000;">
            android:layout_marginLeft</span><span style="color: #0000ff;">="17dp"</span><span style="color: #ff0000;">
            android:drawableLeft</span><span style="color: #0000ff;">="@id/tv_head"</span><span style="color: #ff0000;">
            android:background</span><span style="color: #0000ff;">="@drawable/back"</span> <span style="color: #0000ff;">/></span>
        
    <span style="color: #0000ff;"></span><span style="color: #800000;">RelativeLayout</span><span style="color: #0000ff;">></span></span></span></span>
Copy after login

下面贴出android客户端代码,三个类,一个用于与服务器交互发送post请求,以及json的传递。还有一个Dailog实例。

<span style="color: #0000ff;">package</span><span style="color: #000000;"> com.android.up;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.util.ArrayList;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.util.List;

</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.apache.http.HttpResponse;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.apache.http.NameValuePair;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.apache.http.client.ClientProtocolException;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.apache.http.message.BasicNameValuePair;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.json.JSONObject;

</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> com.android.MainActivity;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> com.android.R;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> com.anroid.net.DialogUtil;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> android.app.Activity;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> android.app.ProgressDialog;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> android.content.Intent;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> android.os.AsyncTask;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> android.os.Bundle;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> android.view.View;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> android.view.View.OnClickListener;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> android.widget.Button;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> android.widget.EditText;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> android.widget.TextView;

</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span> up <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Activity {

    </span><span style="color: #008000;">//</span><span style="color: #008000;"> Progress Dialog</span>
    <span style="color: #0000ff;">private</span><span style="color: #000000;"> ProgressDialog pDialog;
    </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> TextView tv_head;
    JSONParser jsonParser </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> JSONParser();
    EditText inputName;
    EditText inputEmail;
    EditText inputDesc;
    Button upback;

    </span><span style="color: #008000;">//</span><span style="color: #008000;"> url to create new product</span>
    <span style="color: #0000ff;">private</span> <span style="color: #0000ff;">static</span> String url_up = "http://10.0.2.2/up/up.php";<span style="color: #008000;"><br>    private static final String TAG_MESSAGE = "message";<br></span>
<span style="color: #000000;">    @Override
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> onCreate(Bundle savedInstanceState) {
        </span><span style="color: #0000ff;">super</span><span style="color: #000000;">.onCreate(savedInstanceState);
        setContentView(R.layout.up);
        tv_head </span>=<span style="color: #000000;"> (TextView)findViewById(R.id.tv_head);
        tv_head.setText(</span>"建议"<span style="color: #000000;">);
        </span><span style="color: #008000;">//</span><span style="color: #008000;"> Edit Text</span>
        inputName =<span style="color: #000000;"> (EditText) findViewById(R.id.inputName);
        inputEmail </span>=<span style="color: #000000;"> (EditText) findViewById(R.id.inputEmail);
        inputDesc </span>=<span style="color: #000000;"> (EditText) findViewById(R.id.inputDesc);
        upback </span>=<span style="color: #000000;"> (Button)findViewById(R.id.upback);
        upback.setOnClickListener(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> OnClickListener() {
            
            @Override
            </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> onClick(View arg0) {
                </span><span style="color: #008000;">//</span><span style="color: #008000;"> TODO Auto-generated method stub</span>
                Intent back = <span style="color: #0000ff;">new</span> Intent(up.<span style="color: #0000ff;">this</span>,MainActivity.<span style="color: #0000ff;">class</span><span style="color: #000000;">);
                back.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
                startActivity(back);
                up.</span><span style="color: #0000ff;">this</span><span style="color: #000000;">.finish();
            }
        });

        </span><span style="color: #008000;">//</span><span style="color: #008000;"> Create button</span>
        Button btnCreateProduct =<span style="color: #000000;"> (Button) findViewById(R.id.btnCreateProduct);
        </span><span style="color: #008000;">//</span><span style="color: #008000;"> button click event</span>
        btnCreateProduct.setOnClickListener(<span style="color: #0000ff;">new</span><span style="color: #000000;"> View.OnClickListener() {

            </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> onClick(View view) {
                </span><span style="color: #008000;">//</span><span style="color: #008000;"> creating new product in background thread</span>
                <span style="color: #0000ff;">if</span><span style="color: #000000;">(validate()){                
                </span><span style="color: #0000ff;">new</span><span style="color: #000000;"> Up().execute();            
            }
                }
        });
    }
    </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">boolean</span><span style="color: #000000;"> validate()
    {
        String description </span>=<span style="color: #000000;"> inputDesc.getText().toString().trim();
        </span><span style="color: #0000ff;">if</span> (description.equals(""<span style="color: #000000;">))
        {
            DialogUtil.showDialog(</span><span style="color: #0000ff;">this</span>, "您还没有填写建议", <span style="color: #0000ff;">false</span><span style="color: #000000;">);
            </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
        }
        
        </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
    }
    </span><span style="color: #008000;">/**</span><span style="color: #008000;">
     * Background Async Task to Create new product
     * </span><span style="color: #008000;">*/</span>
    <span style="color: #0000ff;">class</span> Up <span style="color: #0000ff;">extends</span> AsyncTask<string string><span style="color: #000000;"> {

        </span><span style="color: #008000;">/**</span><span style="color: #008000;">
         * Before starting background thread Show Progress Dialog
         * </span><span style="color: #008000;">*/</span><span style="color: #000000;">
        @Override
        </span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> onPreExecute() {
            </span><span style="color: #0000ff;">super</span><span style="color: #000000;">.onPreExecute();
            pDialog </span>= <span style="color: #0000ff;">new</span> ProgressDialog(up.<span style="color: #0000ff;">this</span><span style="color: #000000;">);
            pDialog.setMessage(</span>"正在上传.."<span style="color: #000000;">);
            pDialog.setIndeterminate(</span><span style="color: #0000ff;">false</span><span style="color: #000000;">);
            pDialog.setCancelable(</span><span style="color: #0000ff;">true</span><span style="color: #000000;">);
            pDialog.show();
        }
        </span><span style="color: #008000;">/**</span><span style="color: #008000;">
         * Creating product
         * </span><span style="color: #008000;">*/</span>
        <span style="color: #0000ff;">protected</span><span style="color: #000000;"> String doInBackground(String... args) {
            String name </span>=<span style="color: #000000;"> inputName.getText().toString();
            String email </span>=<span style="color: #000000;"> inputEmail.getText().toString();
            String description </span>=<span style="color: #000000;"> inputDesc.getText().toString();

            </span><span style="color: #008000;">//</span><span style="color: #008000;"> Building Parameters</span>
            List<namevaluepair> params = <span style="color: #0000ff;">new</span> ArrayList<namevaluepair><span style="color: #000000;">();
            params.add(</span><span style="color: #0000ff;">new</span> BasicNameValuePair("name"<span style="color: #000000;">, name));
            params.add(</span><span style="color: #0000ff;">new</span> BasicNameValuePair("email"<span style="color: #000000;">, email));
            params.add(</span><span style="color: #0000ff;">new</span> BasicNameValuePair("description"<span style="color: #000000;">, description));

            </span><span style="color: #008000;">//</span><span style="color: #008000;"> getting JSON Object
            </span><span style="color: #008000;">//</span><span style="color: #008000;"> Note that create product url accepts POST method</span>
           <span style="color: #0000ff;">try</span><span style="color: #000000;">{
            JSONObject json </span>=<span style="color: #000000;"> jsonParser.makeHttpRequest(url_up,
                    </span>"POST"<span style="color: #000000;">, params);<br>            String message = json.getString(TAG_MESSAGE);<br>            return message;
           }</span><span style="color: #0000ff;">catch</span><span style="color: #000000;">(Exception e){</span><span style="color: #000000;">
               e.printStackTrace(); <br>               return "";          
           }
            </span><span style="color: #008000;">//</span><span style="color: #008000;"> check for success tag</span>
            
     <span style="color: #000000;">
        }
        </span><span style="color: #008000;">/**</span><span style="color: #008000;">
         * After completing background task Dismiss the progress dialog
         * *</span><span style="color: #008000;">*/</span>
        <span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> onPostExecute(String message) {               </span><span style="color: #000000;">     
            pDialog.dismiss();<br>           //message 为接收doInbackground的返回值<br>            Toast.makeText(getApplicationContext(), message, 8000).show();    
                
               
        }
        }

    }</span></namevaluepair></namevaluepair></string>
Copy after login

下面贴出Dailog实例类

<span style="color: #008000;">/**</span><span style="color: #008000;">
 * 
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">package</span><span style="color: #000000;"> com.anroid.net;

</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> android.app.AlertDialog;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> android.content.Context;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> android.content.DialogInterface;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> android.content.DialogInterface.OnClickListener;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> android.view.View;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> android.app.Activity;</span>
<span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> DialogUtil
{
    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 定义一个显示消息的对话框</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> showDialog(<span style="color: #0000ff;">final</span><span style="color: #000000;"> Context ctx
        , String msg , </span><span style="color: #0000ff;">boolean</span><span style="color: #000000;"> closeSelf)
    {
        </span><span style="color: #008000;">//</span><span style="color: #008000;"> 创建一个AlertDialog.Builder对象</span>
        AlertDialog.Builder builder = <span style="color: #0000ff;">new</span><span style="color: #000000;"> AlertDialog.Builder(ctx)
            .setMessage(msg).setCancelable(</span><span style="color: #0000ff;">false</span><span style="color: #000000;">);
        </span><span style="color: #0000ff;">if</span><span style="color: #000000;">(closeSelf)
        {
            builder.setPositiveButton(</span>"确定", <span style="color: #0000ff;">new</span><span style="color: #000000;"> OnClickListener()
            {
                </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> onClick(DialogInterface dialog, <span style="color: #0000ff;">int</span><span style="color: #000000;"> which)
                {
                    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 结束当前Activity</span>
<span style="color: #000000;">                    ((Activity)ctx).finish();
                }
            });        
        }
        </span><span style="color: #0000ff;">else</span><span style="color: #000000;">
        {
            builder.setPositiveButton(</span>"确定", <span style="color: #0000ff;">null</span><span style="color: #000000;">);
        }
        builder.create().show();
    }    
    </span><span style="color: #008000;">//</span><span style="color: #008000;"> 定义一个显示指定组件的对话框</span>
    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> showDialog(Context ctx , View view)
    {
        AlertDialog.Builder builder </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> AlertDialog.Builder(ctx)
            .setView(view).setCancelable(</span><span style="color: #0000ff;">false</span><span style="color: #000000;">)
            .setPositiveButton(</span>"确定", <span style="color: #0000ff;">null</span><span style="color: #000000;">);
        builder.create()
            .show();
    }
}</span>
Copy after login

剩下就是如何与服务器端交互了不多说,代码如下

<span style="color: #0000ff;">package</span><span style="color: #000000;"> com.android.up;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.io.BufferedReader;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.io.IOException;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.io.InputStream;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.io.InputStreamReader;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.io.UnsupportedEncodingException;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.util.List;

</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.apache.http.HttpEntity;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.apache.http.HttpResponse;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.apache.http.NameValuePair;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.apache.http.client.ClientProtocolException;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.apache.http.client.entity.UrlEncodedFormEntity;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.apache.http.client.methods.HttpPost;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.apache.http.impl.client.DefaultHttpClient;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.apache.http.protocol.HTTP;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.json.JSONException;
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> org.json.JSONObject;

</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> android.util.Log;

</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> JSONParser {

    </span><span style="color: #0000ff;">static</span> InputStream is = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">static</span> JSONObject jObj = <span style="color: #0000ff;">null</span><span style="color: #000000;">;
    </span><span style="color: #0000ff;">static</span> String json = ""<span style="color: #000000;">;
    </span><span style="color: #008000;">//</span><span style="color: #008000;"> constructor</span>
    <span style="color: #0000ff;">public</span><span style="color: #000000;"> JSONParser() {
    }
    </span><span style="color: #008000;">//</span><span style="color: #008000;"> function get json from url
    </span><span style="color: #008000;">//</span><span style="color: #008000;"> by making HTTP POST </span>
    <span style="color: #0000ff;">public</span><span style="color: #000000;"> JSONObject makeHttpRequest(String url, String method,
            List</span><namevaluepair><span style="color: #000000;"> params) {

        </span><span style="color: #008000;">//</span><span style="color: #008000;"> Making HTTP request</span>
        <span style="color: #0000ff;">try</span><span style="color: #000000;"> {    
                </span><span style="color: #008000;">//</span><span style="color: #008000;"> request method is POST
                </span><span style="color: #008000;">//</span><span style="color: #008000;"> defaultHttpClient</span>
                DefaultHttpClient httpClient = <span style="color: #0000ff;">new</span><span style="color: #000000;"> DefaultHttpClient();
                HttpPost httpPost </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> HttpPost(url);
                httpPost.setEntity(</span><span style="color: #0000ff;">new</span><span style="color: #000000;"> UrlEncodedFormEntity(params,HTTP.UTF_8));
                HttpResponse httpResponse </span>=<span style="color: #000000;"> httpClient.execute(httpPost);
                HttpEntity httpEntity </span>=<span style="color: #000000;"> httpResponse.getEntity();
                is </span>=<span style="color: #000000;"> httpEntity.getContent();                
            
        } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (UnsupportedEncodingException e) {
            e.printStackTrace();
        } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (ClientProtocolException e) {
            e.printStackTrace();
        } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (IOException e) {
            e.printStackTrace();
        }
        </span><span style="color: #0000ff;">try</span><span style="color: #000000;"> {
            BufferedReader reader </span>= <span style="color: #0000ff;">new</span> BufferedReader(<span style="color: #0000ff;">new</span><span style="color: #000000;"> InputStreamReader(
                    is, </span>"UTF-8"<span style="color: #000000;">));
            StringBuilder sb </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> StringBuilder();
            String line </span>= <span style="color: #0000ff;">null</span><span style="color: #000000;">;
            </span><span style="color: #0000ff;">while</span> ((line = reader.readLine()) != <span style="color: #0000ff;">null</span><span style="color: #000000;">) {
                sb.append(line </span>+ "\n"<span style="color: #000000;">);
            }
            is.close();
            json </span>=<span style="color: #000000;"> sb.toString();
        } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (Exception e) {
            Log.e(</span>"Buffer Error", "Error converting result " +<span style="color: #000000;"> e.toString());
            Log.d(</span>"json"<span style="color: #000000;">, json.toString());
        }

        </span><span style="color: #008000;">//</span><span style="color: #008000;"> try parse the string to a JSON object</span>
        <span style="color: #0000ff;">try</span><span style="color: #000000;"> {
            jObj </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> JSONObject(json);
        } </span><span style="color: #0000ff;">catch</span><span style="color: #000000;"> (JSONException e) {
            Log.e(</span>"JSON Parser", "Error parsing data " +<span style="color: #000000;"> e.toString());
        }

        </span><span style="color: #008000;">//</span><span style="color: #008000;"> return JSON String</span>
        <span style="color: #0000ff;">return</span><span style="color: #000000;"> jObj;

    }
}</span></namevaluepair>
Copy after login

到此android客户端已经完成,后天服务器端用php+mysql实现,当然这里只是个实例,存取到数据库里面,没有进行展示,代码如下

<span style="color: #000000;">php
</span><span style="color: #008000;">//</span><span style="color: #008000;"> array for JSON response</span>
<span style="color: #800080;">$response</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">();
</span><span style="color: #0000ff;">include</span>("conn.php"<span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;"> check for required fields</span>
<span style="color: #0000ff;">if</span> (<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_POST</span>['name']) && <span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_POST</span>['email']) && <span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_POST</span>['description'<span style="color: #000000;">])) {
    
    </span><span style="color: #800080;">$name</span> = <span style="color: #800080;">$_POST</span>['name'<span style="color: #000000;">];
    </span><span style="color: #800080;">$email</span> = <span style="color: #800080;">$_POST</span>['email'<span style="color: #000000;">];
    </span><span style="color: #800080;">$description</span> = <span style="color: #800080;">$_POST</span>['description'<span style="color: #000000;">];
    </span><span style="color: #800080;">$result</span> = <span style="color: #008080;">mysql_query</span>("INSERT INTO up(name, email, description) VALUES('<span style="color: #800080;">$name</span>', '<span style="color: #800080;">$email</span>', '<span style="color: #800080;">$description</span>')"<span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;"> check if row inserted or not</span>
    <span style="color: #0000ff;">if</span> (<span style="color: #800080;">$result</span><span style="color: #000000;">) {
        </span><span style="color: #008000;">//</span><span style="color: #008000;"> successfully inserted into database</span>
        <span style="color: #800080;">$response</span>["success"] = 1<span style="color: #000000;">;
        </span><span style="color: #800080;">$response</span>["message"] = "Product successfully created."<span style="color: #000000;">;

        </span><span style="color: #008000;">//</span><span style="color: #008000;"> echoing JSON response</span>
        <span style="color: #0000ff;">echo</span> json_encode(<span style="color: #800080;">$response</span><span style="color: #000000;">);
    } </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
        </span><span style="color: #008000;">//</span><span style="color: #008000;"> failed to insert row</span>
        <span style="color: #800080;">$response</span>["success"] = 0<span style="color: #000000;">;
        </span><span style="color: #800080;">$response</span>["message"] = "Oops! An error occurred."<span style="color: #000000;">;
        
        </span><span style="color: #008000;">//</span><span style="color: #008000;"> echoing JSON response</span>
        <span style="color: #0000ff;">echo</span> json_encode(<span style="color: #800080;">$response</span><span style="color: #000000;">);
    }
} </span><span style="color: #0000ff;">else</span><span style="color: #000000;"> {
    </span><span style="color: #008000;">//</span><span style="color: #008000;"> required field is missing</span>
    <span style="color: #800080;">$response</span>["success"] = 0<span style="color: #000000;">;
    </span><span style="color: #800080;">$response</span>["message"] = "Required field(s) is missing"<span style="color: #000000;">;

    </span><span style="color: #008000;">//</span><span style="color: #008000;"> echoing JSON response</span>
    <span style="color: #0000ff;">echo</span> json_encode(<span style="color: #800080;">$response</span><span style="color: #000000;">);
}
</span>?>
Copy after login

数据库表结构如下,连接数据库代码就不贴出了,记得把编码设置为UTF-8就行了。

到此就完成了一个用户反馈的基本功能,后台数据里展示。如有问题欢迎给我留言。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template