0

我正在尝试将用户的消息插入 xml 文件数据库。

我的客户端在 android 上,服务器在 php.Php 上负责在 xml 中插入数据。

用户在文本框中写下他的消息并按下提交,文本被插入到列表视图中一次,但多次插入到数据库中。

我尝试php通过传递我从 android 应用程序发送的相同参数直接在 xml 文件中插入值,它工作正常。它只插入一次值。

这是在列表视图中插入的提交按钮的代码以及asynctask在数据库中插入的调用:

final Button Submit= (Button) findViewById(R.id.SubmitButton);
            Submit.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {

                        String     data=chatbox.getText().toString();

                        HashMap<String,String> hashMap=new HashMap<String,String>();    
                        hashMap.put("You",chatbox.getText().toString());    //insert username(You) value in chatbox to hashmap

                        hashMap.put(HandleJSON.Key_username, "You");
                        hashMap.put(HandleJSON.Key_messageText, data);
                        hashMap.put(HandleJSON.Key_messageDate, UserAdminChatActivity.LastShowingChatDate);

                        HandleJSON.newObject=false;
                        adapter.hashmap.add(adapter.hashmap.size(), hashMap);
                        adapter.notifyDataSetChanged();

                        new InsertAdminChat().execute();

         }});

这是asynctask调用httprequest函数在数据库中插入数据的类。

     public class InsertAdminChat extends AsyncTask<Void,Void,Void>{

            @Override
            protected Void doInBackground(Void... void1) {
                sendHttpRequest("set", "admin", "You" ,chatbox.getText().toString(),clientEmail,"");                    
                return null;
            }
     }

这是 httpRequest 函数:

private String sendHttpRequest(String action,String callFrom,String userName,String chatText,String email,String dateChat)
 {
        httpRequest=new HttpRequest(action,callFrom,userName,chatText,email,dateChat);  
        String data=httpRequest.ExecuteRequest();
        Log.i("mojiiiiii",data);
        httpRequest.ExecuteRequest();
        return data;
 }

这是我的HTTPREQUEST类,由 httprequest 函数调用,用于在数据库中插入数据。

public class HttpRequest {

private final String action;
private final String callFrom;
private final String userName;
private final String chatText;
private final String email;
private final String dateChat;

private HttpClient httpclient;
private HttpGet httpget;
private HttpResponse response ;
private HttpEntity entity;

public HttpRequest(String action,String callFrom,String userName,String chatText,String email,String dateChat)
{


    this.action=action;
    this.callFrom=callFrom;
    this.chatText=chatText;
    this.email=email;
    this.userName=userName; 
    this.dateChat=dateChat;

        httpclient= new DefaultHttpClient();
        httpget = new HttpGet("http://10.116.25.189/php/Chat/xmlManipulator.php?" +
                                                        "action="+this.action+
                                                        "&username="+this.userName+
                                                        "&chatText="+this.chatText+
                                                        "&email="+this.email+
                                                        "&callfrom="+this.callFrom+
                                                        "&dateChatToRetrieve="+URLEncoder.encode(this.dateChat));   //test purposes k liye muazzam
}

public String ExecuteRequest()
{
    try {
    response = httpclient.execute(httpget);
    entity=response.getEntity();

    if(entity!=null)
    {
              InputStream inputStream=entity.getContent();
              String result= convertStreamToString(inputStream);
              Log.i("finalAnswer",result);
              return result;
    }
}
catch (ClientProtocolException e) 
{

    Log.e("errorhai",e.getMessage());
}
catch (IOException e) 
{
    Log.e("errorhai",e.getMessage());
}
    return "";
}

private static String convertStreamToString(InputStream is) {

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");

        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
 }     



}
4

0 回答 0