[英]how can i send data from editText of dialog box whose context is in mainactivity using json object?
While submitting data from editTexts of Dialog box whose context is in Home activity, the submitted button doing nothing.从上下文在 Home 活动中的对话框的 editTexts 提交数据时,提交的按钮什么也不做。
host is a button on home activity,when clicked opens up a dialog box in same activity consists of two editText field (ename,eemail) and submit, cancel button. host 是 home Activity 上的一个按钮,单击时会在同一 Activity 中打开一个对话框,该对话框由两个 editText 字段(ename、eemail)和提交、取消按钮组成。 On submit getFeedback() functions called (used from ApiInterface method)在提交时调用 getFeedback() 函数(从 ApiInterface 方法使用)
host.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view)
{
final Dialog d=new Dialog(context);
d.setCanceledOnTouchOutside(false);
d.setContentView(R.layout.hostdialog);
d.getWindow().setBackgroundDrawableResource(R.drawable.bgdialog);
final EditText ename=(EditText)d.findViewById(R.id.ename);
final EditText eemail=(EditText)d.findViewById(R.id.eemail);
final Button submit=(Button)d.findViewById(R.id.submit);
final Button clear=(Button)d.findViewById(R.id.clear);
submit.setOnClickListener(new View.OnClickListener() {
@SuppressLint("WrongConstant")
@Override
public void onClick(View view) {
try {
getFeedbacks(ename.getText().toString(),eemail.getText().toString());
d.dismiss();
Toast t = Toast.makeText(getApplicationContext()," ✓ Thank You, We'll contact you soon",Toast.LENGTH_LONG);
View v = t.getView();
v.setBackgroundResource(R.drawable.clear);
t.show();
} catch (Exception e) {
e.printStackTrace();
}
}
});
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
d.dismiss();
}
});
d.show();
}
});
and my ApiInterface code is:我的 ApiInterface 代码是:
@GET("myurl")
Call<JsonElement> getFeedbacks(@Body RequestBody requestBody);
getFeedback method consist of:- getFeedback 方法包括:-
private void getFeedbacks(String user_name, String user_email){
JSONObject paramobject = new JSONObject();
try {
paramobject.put("user_name",user_name);
paramobject.put("user_email",user_email);
} catch (JSONException e) {
e.printStackTrace();
}
RequestBody body = RequestBody.create(MediaType.parse("application/json,charset=utf-8"),paramobject.toString());
Call<JsonElement> call = retrofitAPI.getFeedbacks(body);
call.enqueue(new Callback<JsonElement>() {
@Override
public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
try {
JSONArray jsonArray = new JSONArray(response.body().toString());
JSONObject jsonObject = new JSONObject();
for (int n = 0; n < jsonArray.length(); n++){
jsonObject = jsonArray.getJSONObject(n);
String successval = jsonObject.optString("success");
if(successval.equals("0")){
Toast t=Toast.makeText(getApplicationContext()," Request Failed, Try Again",Toast.LENGTH_LONG);
View v=t.getView();
v.setBackgroundResource(R.drawable.clear);
t.show();
}else {
Toast t=Toast.makeText(getApplicationContext()," ✓ Thank You, We'll contact you soon",Toast.LENGTH_LONG);
View v=t.getView();
v.setBackgroundResource(R.drawable.clear);
t.show();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<JsonElement> call, Throwable t) {
Toast.makeText(Home.this,"Error,Try Again",Toast.LENGTH_LONG).show();
}
});
}
Button does not works.按钮不起作用。 Please provide appropriate solution.请提供适当的解决方案。
now i alter a little bit code as i change the call anonymously within method.现在我更改了一些代码,因为我在方法中匿名更改了调用。 but again no work但又没有工作
host.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final Dialog d = new Dialog(context);
d.setCanceledOnTouchOutside(false);
d.setContentView(R.layout.hostdialog);
d.getWindow().setBackgroundDrawableResource(R.drawable.bgdialog);
final EditText ename = (EditText) d.findViewById(R.id.ename);
final EditText eemail = (EditText) d.findViewById(R.id.eemail);
final Button submit = (Button) d.findViewById(R.id.submit);
final Button clear = (Button) d.findViewById(R.id.clear);
submit.setOnClickListener(new View.OnClickListener() {
@SuppressLint("WrongConstant")
@Override
public void onClick(View view) {
try {
sendDataToServer();
d.dismiss();
Toast t = Toast.makeText(getApplicationContext(), " ✓ Thank You, We'll contact you soon", Toast.LENGTH_LONG);
View v = t.getView();
v.setBackgroundResource(R.drawable.clear);
t.show();
} catch (Exception e) {
e.printStackTrace();
}
}
private void sendDataToServer() {
JSONObject paramobject = new JSONObject();
try {
paramobject.put("comment", ename);
paramobject.put("email", eemail);
} catch (JSONException e) {
e.printStackTrace();
}
RequestBody body = RequestBody.create(MediaType.parse("application/json,charset=utf-8"), paramobject.toString());
Call<JsonElement> call = retrofitAPI.getFuturehosts((RequestBody) body);
call.enqueue(new Callback<JsonElement>() {
@Override
public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
try {
JSONArray jsonArray = new JSONArray(response.body().toString());
JSONObject jsonObject = new JSONObject();
for (int n = 0; n < jsonArray.length(); n++) {
jsonObject = jsonArray.getJSONObject(n);
String successval = jsonObject.optString("success");
if (successval.equals("0")) {
Toast t = Toast.makeText(getApplicationContext(), " Request Failed, Try Again", Toast.LENGTH_LONG);
View v = t.getView();
v.setBackgroundResource(R.drawable.clear);
t.show();
} else {
Toast t = Toast.makeText(getApplicationContext(), " ✓ Thank You, We'll contact you soon", Toast.LENGTH_LONG);
View v = t.getView();
v.setBackgroundResource(R.drawable.clear);
t.show();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<JsonElement> call, Throwable t) {
Toast.makeText(Home.this, "Error,Try Again", Toast.LENGTH_LONG).show();
}
});
}
});
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
d.dismiss();
}
});
d.show();
}
});
Check your hostdialog.xml file检查您的 hostdialog.xml 文件
-button id is "submit" or not -button id 是否为“提交”
may be that id is diffrent so the code is not working可能是 id 不同所以代码不起作用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.