我正在调用register api,但是当我在模拟器中运行它时显示成功,但数据不会插入数据库中。请建议我做什么完美,以及我如何从服务器端获得响应......请给我这个想法。
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText)findViewById(R.id.e_username);
email = (EditText)findViewById(R.id.e_email);
password = (EditText)findViewById(R.id.e_password);
register = (Button)findViewById(R.id.btn) ;
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GetCheckEditTextIsEmptyOrNot();
if (CheckEditText) {
SendDataToServer(Getname, GetEmail, GetPassword);
} else {
Toast.makeText(MainActivity.this, "Please fill all form fields.", Toast.LENGTH_LONG).show();
}
}
});
}
public void GetCheckEditTextIsEmptyOrNot(){
Getname = name.getText().toString();
GetEmail = email.getText().toString();
GetPassword = password.getText().toString();
if(TextUtils.isEmpty(Getname) || TextUtils.isEmpty(GetEmail) || TextUtils.isEmpty(GetPassword))
{
CheckEditText = false;
}
else {
CheckEditText = true ;
}
}
public void SendDataToServer(final String patient_firstname, final String patient_email, final String patient_password){
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String QuickName = patient_firstname ;
String QuickEmail = patient_email ;
String QuickPassword = patient_password;
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name", QuickName));
nameValuePairs.add(new BasicNameValuePair("email", QuickEmail));
nameValuePairs.add(new BasicNameValuePair("password", QuickPassword));
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(DataParseUrl);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpClient.execute(httpPost);
// Log.d("reponse","error : "+d.toString);
HttpEntity entity = response.getEntity();
} catch (ClientProtocolException e) {
Log.e("log_tag","error : "+e.toString());
} catch (IOException e) {
Log.e("log_tag","error : "+e.toString());
}
// return "Data Submit Successfully";
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 + "\n");
}
is.close();
json = sb.toString();
Log.d("JSON String",json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
return "Data Submit Successfully";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(MainActivity.this, "Data Submit Successfully", Toast.LENGTH_LONG).show();
}
}
SendPostReqAsyncTask sendPostReqAsyncTask = new SendPostReqAsyncTask();
sendPostReqAsyncTask.execute(patient_firstname, patient_email, patient_password);
}
}
我也没有在logcat中收到任何错误。
答案 0 :(得分:0)
它像你一样使用post执行消息它总是会在后台执行完毕后显示。无论什么是背景中的代码。首先你看到你的api是正确使用任何brouser命中api所有参数如果数据存储响应任何json catch jsone和parsh json数据和消息当json发送成功按摩