我正在尝试将用户的消息插入到xml文件数据库中。
我的客户端在php上运行android和服务器.Php负责在xml中插入数据。
用户在文本框中写入他的消息并按提交文本一次插入列表视图但在数据库中多次插入。
我已经尝试通过php
通过传递我从Android应用程序发送的相同参数直接插入xml文件中的值。它工作正常。它只插入一次值。
这是提交按钮的代码,它插入listview并调用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();
}
}