我有一个问题,我的progressdialog,我的应用程序从json搜索数据,然后对话框来了,但我在收集数据的时候想要它。我的错在哪里?
有编辑的代码 logcat(致命的例外:主要 显示java.lang.NullPointerException) JSON([{“name”:“Test”}]) 编辑:
public class MainActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new TheTask().execute();
}
class TheTask extends AsyncTask<Void, Void, JSONObject> {
InputStream is = null;
String result = "";
JSONObject jArray = null;
ProgressDialog pd;
@Override
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
pd.dismiss();
JSONObject jObject = result;
try {
String aJsonString = jObject.getString("name");
} catch (JSONException e) {
e.printStackTrace();
}
setListAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, (List<String>) result));
// parse and set List adapter here
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = ProgressDialog.show(MainActivity.this, "dialog title",
"dialog message", true);
}
@Override
protected JSONObject doInBackground(Void... arg0) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("***");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// Convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
jArray = new JSONObject(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;
}
}
答案 0 :(得分:0)
Thread.sleep(50);
你正在阻止它的ui线程上调用sleep。删除它。
使用AsyncTask
代替Thread
。
http://developer.android.com/reference/android/os/AsyncTask.html
在onPreExecute
中显示进度对话框。在doInbackground
中处理您的http请求。在onPostExecute
中关闭对话框并相应地更新ui。
调用
new TheTask().execute();
然后
class TheTask extends AsyncTask<Void, Void, Void> {
ProgressDialog pd;
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progress.dismiss();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progress = ProgressDialog.show(MainActivity.this, "dialog title",
"dialog message", true);
}
@Override
protected Void doInBackground(Void... arg0) {
// http request
return null;
}
}
编辑:
public class MainActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new TheTask().execute();
}
class TheTask extends AsyncTask<Void, Void, JSONObject> {
InputStream is = null;
String result = "";
JSONObject jArray = null;
ProgressDialog pd;
@Override
protected void onPostExecute(JSONObject result) {
super.onPostExecute(result);
pd.dismiss();
// parse and set List adapter here
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = ProgressDialog.show(MainActivity.this, "dialog title",
"dialog message", true);
}
@Override
protected JSONObject doInBackground(Void... arg0) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("******");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// Convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
jArray = new JSONObject(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;
}
}
}
EDIT2:
class TheTask extends AsyncTask<Void, Void, JSONArray> {
InputStream is = null;
String result = "";
JSONArray jArray = null;
ProgressDialog pd;
@Override
protected void onPostExecute(JSONArray result) {
super.onPostExecute(result);
pd.dismiss();
ArrayList<String> list= new ArrayList<String>();
try
{
for(int i=0;i<result.length();i++)
{
JSONObject jb = result.getJSONObject(i) ;
String name = jb.getString("name");
list.add(name);
}
}catch(Exception e)
{
e.printStackTrace();
}
setListAdapter(new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, list));
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = ProgressDialog.show(MainActivity.this, "dialog title",
"dialog message", true);
}
@Override
protected JSONArray doInBackground(Void... arg0) {
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("******");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// Convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
jArray = new JSONArray(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;
}
}
}
答案 1 :(得分:0)
您在Thread.sleep(50)
上致电UI Thread
,以便UI
冻结,直至完成。将您的操作移至AsyncTask
中的sleep()
和doInBackground()
。将所有网络代码也放在doInBackground()
中。
您可以使用AsyncTasks
其他方法更新UI
。