0
APIを使用して新規ユーザーを登録する「ユーザー登録フォーム」をビルドしました。私はこれをAsyncTask "POST"メソッドで構築します。しかし、私のアプリでは、ユーザーでさえ登録トーストではない "登録成功"メッセージが付属しています。 Toastを使って "registration unsuccessfull"や他のメッセージが表示されたが、トーストメッセージをどこに設定するのかわからない、適切なエラーメッセージを表示したい。これは私のAsyncTaskコードです...android studioのAsyncTaskに適切なエラーメッセージを設定するにはどうすればいいですか?
public class AsyncCreateacc extends AsyncTask<Void, Void, AsyncTaskResult<Object>>{
private String user_name = "";
private String passwordacc= "";
@Override
protected void onPreExecute() {
user_name=username.getText().toString();
passwordacc=password.getText().toString();
super.onPreExecute();
}
@Override
protected AsyncTaskResult<Object> doInBackground(Void... params) {
BufferedReader in = null;
try {
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 20000);
HttpConnectionParams.setSoTimeout(httpParameters, 20000);
// 1. create HttpClient
HttpClient client = new DefaultHttpClient(httpParameters);
String url = "My_URL";
// 2. make POST request to the given URL
HttpPost request = new HttpPost(url);
// 3. Set some headers to inform server about the type of the content
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
request.setHeader("Authorization", "Basic " + "My_key");
// 4. set httpPost Entity
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("email", user_name);
jsonObject.accumulate("password",passwordacc);
jsonObject.accumulate("company_id", "0");
jsonObject.accumulate("user_type", "C");
request.setEntity(new StringEntity(jsonObject.toString(), "UTF-8"));
//
// 5. Execute POST request to the given URL
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
String result = sb.toString();
return new AsyncTaskResult<Object>(result);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return new AsyncTaskResult<Object>("");
}
@Override
protected void onPostExecute(AsyncTaskResult<Object> result) {
super.onPostExecute(result);
// findViewById(R.id.rlProgress).setVisibility(View.GONE);
if (result.getError() != null) {
Toast.makeText(getApplicationContext(), "Register Unsuccessfull", Toast.LENGTH_LONG).show();
} else {
try {
// hideSoftKeyboard();
// showSnackBar("Address Updated", findViewById(R.id.activity_edit_address));
Toast.makeText(getApplicationContext(), "Register Successfully", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
お勧めします。ありがとうございました!