我在Android开发方面很陌生,而且我一直试图在最后一天解决这个问题,但我无法弄清楚是什么问题。
上下文mContext = this;在活动的onCreate中完成。
我在LogCat中没有其他信息:threadid = 1:线程退出时未捕获异常(group = 0x418a6c50)
从asyncTask的postExecute调用此方法。 run()中的代码被调用并在log" run launch"之间崩溃。和log"文本集" ...
public void updateUiMethod(final Boolean updateTxt) {
((Activity) mContext).runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// update your UI here and be sure to give the activity context, not getApplicationContext();
if(updateTxt) {
Log.e("mytag","run launched");
credit.setText(creditsValue);
expiration.setText(fidelityValue);
Log.e("mytag","texts set");
if(userStatusText.equals(STATUS_SUBSCRIPTION)) {
// AFFICHER LE INFINITY
Log.e("mytag","first");
statut.setText(mContext.getResources().getString(R.string.credits_userstatus_subscriber));
credit.setText("∞");
} else if (userStatusText.equals(STATUS_DISCOVER)) {
statut.setText(mContext.getResources().getString(R.string.credits_userstatus_discovery));
} else if (userStatusText.equals(STATUS_ONESHOT)) {
statut.setText(mContext.getResources().getString(R.string.credits_userstatus_units));
} else {
statut.setText(mContext.getResources().getString(R.string.credits_userstatus_premium));
}
}
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setTitle(alertTitle);
builder.setMessage(alertMessage);
builder.setPositiveButton("OK",null);
final AlertDialog alert = builder.create();
Log.e("mytag", "alert créée");
alert.show();
}
});
}
这是postExecute:
@Override
protected void onPostExecute(Void params) {
// do something
Log.e("tag",jsonForPostExecute.toString());
try {
if (jsonForPostExecute.getString("error") != null) {
alertMessage = jsonForPostExecute.getString("message");
alertTitle = jsonForPostExecute.getString("messagelabel");
creditsValue = jsonForPostExecute.getInt("credits");
fidelityValue = jsonForPostExecute.getInt("fidelity");
userStatusText = jsonForPostExecute.getString("userstatus");
Log.e("mytag", "pas d'erreur");
updateUiMethod(true);}}}
我尝试了很多东西以避免崩溃,但应用程序仍然崩溃,我现在确定我无法自己理解为什么。
编辑:这里是整个asyncTask类:
public class FetchAddCreditsTask extends AsyncTask<Void, Integer, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
SharedPreferences mPrefs = getApplicationContext()
.getSharedPreferences("compte", 0);
String postContent = "platform=android&email=";
String email = mPrefs.getString("mail", "Error");
postContent = postContent + email + "&nb=";
postContent = postContent + nb + "&token=";
postContent = postContent + token + "&productID=";
postContent = postContent + productID + "&lang=";
String langage = mPrefs.getString("language", "error");
postContent = postContent + langage;
MD5Digest md5 = new MD5Digest();
String hash = "";
try {
hash = md5.getMD5("hash1" + postContent + "hash2");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("email", email));
pairs.add(new BasicNameValuePair("nb", nb));
pairs.add(new BasicNameValuePair("receipt", token));
pairs.add(new BasicNameValuePair("platform", "android"));
pairs.add(new BasicNameValuePair("lang", langage));
pairs.add(new BasicNameValuePair("hash", hash));
pairs.add(new BasicNameValuePair("productid", productID));
//post.setEntity(new UrlEncodedFormEntity(pairs));
postContent = postContent + "&hash=" + hash;
String url = "https://api-prod-1.l-w.mobi/addCredits";
URL obj = new URL(url);
trustAllHosts();
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add request header
con.setRequestMethod("POST");
String userAgent = System.getProperty("http.agent");
con.setRequestProperty("User-Agent", userAgent);
//con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
// Send post request
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(pairs));
writer.flush();
writer.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + postContent);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
JSONObject jsonObject = new JSONObject(response.toString());
// parsing data
jsonForPostExecute = jsonObject;
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(Void params) {
// do something
Log.e("tag",jsonForPostExecute.toString());
try {
if (jsonForPostExecute.getString("error") != null) {
alertMessage = jsonForPostExecute.getString("message");
alertTitle = jsonForPostExecute.getString("messagelabel");
creditsValue = jsonForPostExecute.getInt("credits");
fidelityValue = jsonForPostExecute.getInt("fidelity");
userStatusText = jsonForPostExecute.getString("userstatus");
Log.e("mytag", "pas d'erreur");
updateUiMethod(true);
} else {
final String errorlabel = jsonForPostExecute.getString("errorlabel");
final String errorMessage = jsonForPostExecute.getString("error");
Log.e("mytag", "il y a une erreur");
AlertDialog.Builder builder = new AlertDialog.Builder(Activity_Achat.this);
builder.setTitle(errorlabel);
builder.setMessage(errorMessage);
builder.setPositiveButton("OK",null);
final AlertDialog alert = builder.create();
Activity_Achat.this.runOnUiThread(new java.lang.Runnable(){
public void run(){
//show AlertDialog
alert.show();
}
});
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}