【发布时间】:2015-07-06 18:57:31
【问题描述】:
我有一个 JSONparser 类来获取数据并将其发送到运行良好的服务器,但是在没有 wifi 连接的情况下进行测试时,该过程需要更长的时间。是否可以将一个流程对话框放入我的类中,因为我会将这个类称为每个需要发送或接收数据的活动。
我尝试了一些不同的方法,例如在任务之前和之后应用设置 LinearLayout 的可见性,例如:
loading.setVisibility(View.VISIBLE);
/// DO TASK
loading.setVisibility(View.GONE);
但屏幕只是冻结并加载数据。
我尝试在 HTTP 请求的开头添加一个 processDialog,并在任务完成后再次删除它,但我得到一个空引用错误。
我感觉错误可能在于课程本身,因为我是 Java 新手,目前我只真正了解基础知识,所以只是学习。
这是我的 JSONParser 类
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
static String root = "**MY SERVER**";
private View loading = null;
public JSONParser() {
}
public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) {
params.add(new BasicNameValuePair("APP_ID", "**APPTOKEN**"));
// Making HTTP request
try {
// check for request method
if(method.equalsIgnoreCase("POST")){
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(this.root + url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method.equalsIgnoreCase("GET")){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader( is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
说实话,我并没有写完所有的课程,我遵循tutorial 并根据我的需要修改了课程。
类可以在任何活动中调用,类的思路是这样的:
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
JSONParser jsonParser = new JSONParser();
loading.setVisibility(View.VISIBLE);
JSONObject json = jsonParser.makeHttpRequest("login.php", "POST", params);
try {
Boolean success = json.getBoolean("ok");
loading.setVisibility(View.GONE);
if (success) {
Log.d("LOGIN","LOGIN SUCCESSFUL");
finish();
} else {
String err_msg = json.getString("error");
Toast toast = Toast.makeText(getApplicationContext(), err_msg, Toast.LENGTH_LONG);
toast.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
如果问题出在课程上,我敢肯定,您能否解释一下如何修改它以合并progressDialog。
更新 在以下答案的帮助下,我设法使用 PostAsync 类添加了一个进程对话框。 我已将此类修改为更具动态性,是否可以处理当前活动的返回,或者我只能在 PostAcync 类中处理返回。
例如: 基本 PostAcync 类将由 New PostAcync.execute(param,param); 调用。 但是我已经对此进行了修改,使其具有通用性并且可以用于任何活动; 所以不,我会调用这个类并通过以下方式执行任务:
PostAsync post = new PostAsync();
post.context = ThisActivity.this;
post.message = "Attempting to login";
post.execute("login.php", email, password);
所以现在我添加 Context 以使 Dialog Builder 运行 我可以根据任务添加不同的消息 第一个参数始终是网页。
有没有办法可以在上面添加回调
JSONObject response = post.repsonse;
//then process the data here as I could using the ajax success callback in jQuery
【问题讨论】:
标签: java android json android-asynctask dialog