【发布时间】:2016-02-23 17:14:33
【问题描述】:
大家好,我是 android 新手,目前正在学习 android。我正在为我的应用程序制作登录系统。这是我的异步任务
@Override
protected String doInBackground(String... params) {
BufferedReader in = null;
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("user_email", user.email));
dataToSend.add(new BasicNameValuePair("user_pass", 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 + "login.php");
User returnedUser= null;
try {
post.setEntity(new UrlEncodedFormEntity(dataToSend));
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
in = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
String response = "";
String line = "";
while ((line = in.readLine())!= null){
response+= line;
}
in.close();
Log.d("qwerty", response);
if(response.equals("notAct\t\t")){
return "notAct";
}else if(response.equals("Error\t\t")){
return "Error";
}
String result = EntityUtils.toString(entity);
final JSONObject jObject = new JSONObject(result);
if (jObject.length() == 0) {
progressDialog.dismiss();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Connection Error json");
builder.setPositiveButton("ok", null);
builder.show();
} else {
String user_id = jObject.getString("user_id");
String user_name = jObject.getString("user_name");
returnedUser = new User(user.email, user.password, user_name, user_id);
Log.d("qwerty", "Exception time");
userCallback.done(returnedUser);
}
}catch(NullPointerException e){
e.printStackTrace();
} catch (JSONException e){
e.printStackTrace();
} catch(ArithmeticException e) {
e.printStackTrace();
} catch (ConnectTimeoutException e) {
Log.d("qwerty", "RunTime");
return "Abc";
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
try {
if (result.equals("notAct")) {
//account activated
} else if (result.equals("Error")) {
//email does't exist
}else if (result.equals("Abc")) {
//connection time out
}
}catch(NullPointerException e){
//null pointer exception
}catch (RuntimeException e){
//
} catch (Exception e){
//
}
super.onPostExecute(result);
}
所有异常都被处理,但是这个 asynctask 从 php 文件中获取的 json 和 String 不能一起工作
如果我在读取字符串(字符串响应)之前编写了 Json 的编码,那么 json 被它读取,但它无法读取错误,例如用户输入了错误的用户详细信息。提前感谢您的帮助。
【问题讨论】:
-
EntityUtils.toString(entity)+in.readLine()where (in消费实体) ...所以你期望什么? ...您知道您使用不同的代码两次执行相同的操作吗? -
no coz am new in android and did not get what you're about **EntityUtils.toString(entity) + in.readLine() where (in sumption the entity) **跨度>
-
仅使用
String result = EntityUtils.toString(entity);并摆脱while循环。一旦您阅读了来自 Http 调用的 InputStream 一次,就不能再这样做了。这就是塞尔文的意思 -
非常感谢 Selvin 和 Blackbelt 现在再次感谢它的工作...... :-)
标签: android android-asynctask illegalstateexception asynctaskloader