Android 单击按钮时,异步方法未运行
我的应用程序中有一个按钮,当我点击它时,它声明了方法Android 单击按钮时,异步方法未运行,android,android-asynctask,okhttp,Android,Android Asynctask,Okhttp,我的应用程序中有一个按钮,当我点击它时,它声明了方法insertintodatabase。但是,当我点击它时,什么都没有发生,甚至日志也没有显示任何内容。 问题在哪里?请建议 private final OkHttpClient client = new OkHttpClient(); public void SignUp(View view) { insertToDatabase(); } private void insertToDataba
insertintodatabase
。但是,当我点击它时,什么都没有发生,甚至日志
也没有显示任何内容。
问题在哪里?请建议
private final OkHttpClient client = new OkHttpClient();
public void SignUp(View view)
{
insertToDatabase();
}
private void insertToDatabase(){
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute()
{
name = usernam.getText().toString();
pass = passw.getText().toString();
emails = email.getText().toString();
Log.e("GetText","called");
}
@Override
protected String doInBackground(String... params) {
String json = "";
try {
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("name", name);
jsonObject.accumulate("password", pass);
jsonObject.accumulate("email", emails);
json = jsonObject.toString();
Log.e("MYAPP", "getjson");
} catch (JSONException e) {
Log.e("MYAPP", "unexpected JSON exception", e);
}
try{
RequestBody formBody = new FormEncodingBuilder()
.add("result", json)
.build();
Request request = new Request.Builder()
.url("https://justedhak.comlu.com/receiver.php")
.post(formBody)
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
} catch (IOException e){
Log.e("MYAPP", "unexpected JSON exception", e);
}
return "success";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
您必须在某个地方创建SendPostReqAsyncTask的对象并调用传递参数的execute方法
// Example class
class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
// some code
}
// Call the execute method of Async Task
new DownloadFilesTask().execute(url1, url2, url3);
//示例类
类DownloadFilesTask扩展了AsyncTask{
//一些代码
}
//调用异步任务的execute方法
新建DownloadFilesTask().execute(url1、url2、url3);
//像这样使用
public class <Your Root class> extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
// some code
private final OkHttpClient client = new OkHttpClient();
public void SignUp(View view)
{
new SendPostReqAsyncTask().execute();
}
}
class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute()
{
name = usernam.getText().toString();
pass = passw.getText().toString();
emails = email.getText().toString();
Log.e("GetText","called");
}
@Override
protected String doInBackground(String... params) {
String json = "";
try {
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("name", name);
jsonObject.accumulate("password", pass);
jsonObject.accumulate("email", emails);
json = jsonObject.toString();
Log.e("MYAPP", "getjson");
} catch (JSONException e) {
Log.e("MYAPP", "unexpected JSON exception", e);
}
try{
RequestBody formBody = new FormEncodingBuilder()
.add("result", json)
.build();
Request request = new Request.Builder()
.url("https://justedhak.comlu.com/receiver.php")
.post(formBody)
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
} catch (IOException e){
Log.e("MYAPP", "unexpected JSON exception", e);
}
return "success";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
公共类扩展活动{
@凌驾
创建时的公共void(Bundle savedInstanceState){
//一些代码
私有最终OkHttpClient客户端=新OkHttpClient();
公共无效注册(视图)
{
新建SendPostReqAsyncTask().execute();
}
}
类SendPostReqAsyncTask扩展了AsyncTask{
@凌驾
受保护的void onPreExecute()
{
name=usernam.getText().toString();
pass=passw.getText().toString();
emails=email.getText().toString();
Log.e(“GetText”,“called”);
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串json=“”;
试一试{
JSONObject JSONObject=新的JSONObject();
jsonObject.accumulate(“name”,name);
jsonObject.accumulate(“密码”,pass);
jsonObject.accumulate(“电子邮件”,电子邮件);
json=jsonObject.toString();
Log.e(“MYAPP”、“getjson”);
}捕获(JSONException e){
Log.e(“MYAPP”,“意外JSON异常”,e);
}
试一试{
RequestBody formBody=新FormEncodingBuilder()
.add(“结果”,json)
.build();
Request Request=newrequest.Builder()
.url(“https://justedhak.comlu.com/receiver.php")
.职位(表格)
.build();
Response=client.newCall(request.execute();
如果(!response.issusccessful())抛出新IOException(“意外代码”+响应);
}捕获(IOE异常){
Log.e(“MYAPP”,“意外JSON异常”,e);
}
返回“成功”;
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG.show();
}
}
因为OkHttp支持异步,我想您也可以参考以下方法:
假设您有mHandler=new处理程序(Looper.getMainLooper());
内部onCreate
private void updateToDatabase() {
// POST request
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new FormEncodingBuilder()
.add("key1", "value1")
.add("key2", "value2")
.build();
Request request = new Request.Builder()
.url("http://...")
.post(requestBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(final Request request, final IOException e) {
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(mContext, e.toString(), Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onResponse(Response response) throws IOException {
mHandler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(mContext, response.body().string(), Toast.LENGTH_SHORT).show();
}
});
}
});
}
如果仍要与AsyncTask一起使用,请按以下方式更新代码:
public class MainActivity extends AppCompatActivity {
...
public void SignUp(View view)
{
new SendPostReqAsyncTask().execute();
}
...
private class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
name = usernam.getText().toString();
pass = passw.getText().toString();
emails = email.getText().toString();
Log.e("GetText", "called");
}
@Override
protected String doInBackground(String... params) {
String json = "";
try {
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("name", name);
jsonObject.accumulate("password", pass);
jsonObject.accumulate("email", emails);
json = jsonObject.toString();
Log.e("MYAPP", "getjson");
} catch (JSONException e) {
Log.e("MYAPP", "unexpected JSON exception", e);
}
try {
OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormEncodingBuilder()
.add("result", json)
.build();
Request request = new Request.Builder()
.url("https://justedhak.comlu.com/receiver.php")
.post(formBody)
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
} catch (IOException e) {
Log.e("MYAPP", "unexpected JSON exception", e);
}
return "success";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
}
public类MainActivity扩展了AppCompatActivity{
...
公共无效注册(视图)
{
新建SendPostReqAsyncTask().execute();
}
...
私有类SendPostReqAsyncTask扩展了AsyncTask{
@凌驾
受保护的void onPreExecute(){
name=usernam.getText().toString();
pass=passw.getText().toString();
emails=email.getText().toString();
Log.e(“GetText”,“called”);
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
字符串json=“”;
试一试{
JSONObject JSONObject=新的JSONObject();
jsonObject.accumulate(“name”,name);
jsonObject.accumulate(“密码”,pass);
jsonObject.accumulate(“电子邮件”,电子邮件);
json=jsonObject.toString();
Log.e(“MYAPP”、“getjson”);
}捕获(JSONException e){
Log.e(“MYAPP”,“意外JSON异常”,e);
}
试一试{
OkHttpClient=新的OkHttpClient();
RequestBody formBody=新FormEncodingBuilder()
.add(“结果”,json)
.build();
Request Request=newrequest.Builder()
.url(“https://justedhak.comlu.com/receiver.php")
.职位(表格)
.build();
Response=client.newCall(request.execute();
如果(!response.issusccessful())抛出新IOException(“意外代码”+响应);
}捕获(IOE异常){
Log.e(“MYAPP”,“意外JSON异常”,e);
}
返回“成功”;
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG.show();
}
}
}
为什么不将asynctask类移到insertToDatabase
?然后在insertToDatabase
内部只需调用new SendPostReqAsyncTask()。执行(…);
您需要调用异步任务,在类完成new DownloadFilesTask()后放置此行。执行(url1、url2、url3);
@BNK您在评论中的意思是创建一个名为insertintodatabase.java的新活动,然后从主活动调用它吗?不,只有一个主活动。java@BNK我正在尝试你之前在另一个问题中回答的代码,同时你能解释一下或添加h吗
public class MainActivity extends AppCompatActivity {
...
public void SignUp(View view)
{
new SendPostReqAsyncTask().execute();
}
...
private class SendPostReqAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected void onPreExecute() {
name = usernam.getText().toString();
pass = passw.getText().toString();
emails = email.getText().toString();
Log.e("GetText", "called");
}
@Override
protected String doInBackground(String... params) {
String json = "";
try {
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("name", name);
jsonObject.accumulate("password", pass);
jsonObject.accumulate("email", emails);
json = jsonObject.toString();
Log.e("MYAPP", "getjson");
} catch (JSONException e) {
Log.e("MYAPP", "unexpected JSON exception", e);
}
try {
OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormEncodingBuilder()
.add("result", json)
.build();
Request request = new Request.Builder()
.url("https://justedhak.comlu.com/receiver.php")
.post(formBody)
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
} catch (IOException e) {
Log.e("MYAPP", "unexpected JSON exception", e);
}
return "success";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
}