我有一個通過android的服務器請求的代碼。這是我try塊的片段:我在try塊內的代碼沒有在android中執行。沒有其他線程似乎解釋這
try{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
JSONObject jsonObject = new JSONObject(result);
Toast.makeText(context,jsonObject.length()+"i ran",Toast.LENGTH_SHORT).show();
if(jsonObject.length() != 0){
Log.v("happened", "2");
String dbname = jsonObject.getString("dbname");
String userloggedin = jsonObject.getString("userloggedin");
String username = jsonObject.getString("username");String password = jsonObject.getString("password");
String email = jsonObject.getString("email");String name = jsonObject.getString("name");
String dob = jsonObject.getString("dob"); String mobile = jsonObject.getString("mobile");
String imagelink = jsonObject.getString("imagelink");String country = jsonObject.getString("country");
String currency = jsonObject.getString("currency");String recentcount = jsonObject.getString("recentcount");
String lowoncashlevel = jsonObject.getString("lowoncashlevel");
returnedUser = new User(dbname,Boolean.getBoolean(userloggedin),username,password,name,dob,mobile,email,imagelink,country,currency,Integer.getInteger(recentcount),Integer.getInteger(lowoncashlevel));
}
}catch (Exception e){
e.printStackTrace();
}
我知道,因爲有在後臺運行過一個進度對話框中的麪包會導致錯誤,但它沒有,這就是我發現,try塊是問題出在哪裏。
這是有這個方法的類:
public class FetchUserDataAsyncTask extends AsyncTask<Void,Void,User> {
User user;
GetUserCallBack userCallBack;
public FetchUserDataAsyncTask(User user, GetUserCallBack userCallBack){
this.user=user;
this.userCallBack=userCallBack;
}
@Override
protected User doInBackground(Void... params) {
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("username",user.username));
dataToSend.add(new BasicNameValuePair("password",user.password));
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS + "FetchUserData2.php");
User returnedUser = null;
try{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
JSONObject jsonObject = new JSONObject(result);
Toast.makeText(context,jsonObject.length()+"i ran",Toast.LENGTH_SHORT).show();
if(jsonObject.length() != 0){
Log.v("happened", "2");
String dbname = jsonObject.getString("dbname");
String userloggedin = jsonObject.getString("userloggedin");
String username = jsonObject.getString("username");String password = jsonObject.getString("password");
String email = jsonObject.getString("email");String name = jsonObject.getString("name");
String dob = jsonObject.getString("dob"); String mobile = jsonObject.getString("mobile");
String imagelink = jsonObject.getString("imagelink");String country = jsonObject.getString("country");
String currency = jsonObject.getString("currency");String recentcount = jsonObject.getString("recentcount");
String lowoncashlevel = jsonObject.getString("lowoncashlevel");
returnedUser = new User(dbname,Boolean.getBoolean(userloggedin),username,password,name,dob,mobile,email,imagelink,country,currency,Integer.getInteger(recentcount),Integer.getInteger(lowoncashlevel));
// returnedUser = new User(jsonObject.getString("dbname"),Boolean.getBoolean(jsonObject.getString("userloggedin")),
// jsonObject.getString("username"),jsonObject.getString("password"),jsonObject.getString("name"),
// jsonObject.getString("dob"),jsonObject.getString("mobile"),jsonObject.getString("email"),
// jsonObject.getString("imagelink"),jsonObject.getString("country"),jsonObject.getString("currency"),
// Integer.getInteger(jsonObject.getString("recentcount")),Integer.getInteger(jsonObject.getString("lowoncashlevel")));
}
}catch (Exception e){
e.printStackTrace();
}
return returnedUser;
}
@Override
protected void onPostExecute(User returnedUser) {
progressDialog.dismiss();
userCallBack.done(returnedUser);
super.onPostExecute(returnedUser);
}
}
執行方法是從這個公共方法叫:
public void fetchUserDataInBackground(User user,GetUserCallBack userCallBack){
progressDialog.show();
new FetchUserDataAsyncTask(user,userCallBack).execute();
}
敬酒不從後臺線程運行,並會拋出異常。但是由於您沒有到達那裏,因此您調用FetchUserDataAsyncTask類的方式看起來有錯誤。你能顯示你實際調用它的那一行代碼嗎?我的猜測是,當你打算開始這個類時,你錯過了execute()方法 – ucsunil
@Gogeta是的,我知道吐司會拋出一個異常。我在try塊之前放了一個toast,它引發了一個錯誤,所以我推斷try塊上面的代碼已經到達,但try塊仍然沒有被觸及。 我添加的方法調用質疑 –
您可以將上線新FetchUserDataAsyncTask(.....)斷點,看看是否正在達到該行代碼?乍一看你的try/catch語句似乎很好。所以它必須是調用來啓動沒有運行的AsyncTask。 – ucsunil