gpt4 book ai didi

android - AsyncTask 类中的错误无法发送数据

转载 作者:行者123 更新时间:2023-11-30 04:01:34 25 4
gpt4 key购买 nike

我在 android 平台上工作,想将一些注册详细信息发送到 php 页面,为此我创建了一个名为“ContinueRegister”的 Activity ,它包含一个 AsyncTask 类。在运行我的项目时,它显示不幸的是已经停止并且 logcat 中有一些错误,比如

 Activity com.opz.ContinueRegister has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41269db8     
error opening trace file: No such file or directory
at android.os.Looper.loop(Looper.java:137)
at android.os.Handler.handleCallback(Handler.java:615)
atcom.opz.ContinueRegister$CreateNewUser.onPreExecute(ContinueRegister.java:76)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
at java.lang.reflect.Method.invokeNative(Native Method)

这是我的类(class)文件

public class ContinueRegister extends Activity {

// Progress Dialog
private ProgressDialog pDialog;

JSONParser jsonParser = new JSONParser();

EditText firstname;
EditText lastname;
EditText dob;
EditText gender;

RegisterActivity ra= new RegisterActivity();

// url to create new product
private static String url_create_product = "http://localhost/login_api/create_account.php/";

// JSON Node names
private static final String TAG_SUCCESS = "success";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set View to register.xml
setContentView(R.layout.registerfinsh);

Button bt=(Button)findViewById(R.id.btnRegister);

// Listening to Login Screen link
bt.setOnClickListener(new View.OnClickListener(){

public void onClick(View arg0) {

new CreateNewUser().execute();
}
});

}


/**
* Background Async Task to Create new product
* */

class CreateNewUser extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ContinueRegister.this);
pDialog.setMessage("Creating New User..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}

/**
* Creating User
* */
protected String doInBackground(String... args) {

String Username = ra.username.getText().toString();
String Email = ra.email.getText().toString();
String Password =ra.password.getText().toString();
firstname = (EditText)findViewById(R.id.first_name);
lastname =(EditText)findViewById(R.id.last_name);
dob =(EditText)findViewById(R.id.date_of_birth);
gender = (EditText)findViewById(R.id.gender);
String Firstname = firstname.getText().toString();
String Lastname = lastname.getText().toString();
String Dob =dob.getText().toString();
String Gender =gender.getText().toString();

// Building Parameters

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Username", Username));
params.add(new BasicNameValuePair("Email", Email));
params.add(new BasicNameValuePair("Password", Password));
params.add(new BasicNameValuePair("Firstname", Firstname));
params.add(new BasicNameValuePair("Lastname", Lastname));
params.add(new BasicNameValuePair("Dob", Dob));
params.add(new BasicNameValuePair("Gender", Gender));

// getting JSON Object
// Note that create user url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);

// check log cat for response
Log.d("Create Response", json.toString());

// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);

if (success == 1) {
// successfully created user
Intent i = new Intent(getApplicationContext(), FinishSignupActivity.class);
startActivity(i);

// closing this screen
finish();
} else {
// failed to create user
}
} catch (JSONException e) {
e.printStackTrace();
}

return null;
}

/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();


TextView loginScreen = (TextView) findViewById(R.id.link_to_login2);

loginScreen.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
Intent m = new Intent(getApplicationContext(),LoginActivity.class);
startActivity(m);

}
});
}
}
}

请帮我解决这个错误

最佳答案

String url_create_product = "http://10.0.2.2/login_api/create_account.php/";

使用您的本地 IP,如 192.168.0.1

String url_create_product = "http://192.168.0.1/login_api/create_account.php/";

同时检查您的 login_api 文件夹中是否有名为 create_account.php 的文件

还有一件事是永远不要访问 doinBackGround(...) 中的任何 View

因为doinBackGround方法是非UI线程。

关于android - AsyncTask 类中的错误无法发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12423497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com