есть код
package com.example.json;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private int pageNumber = 1;
private String url = "http://hantim.ru/search.json?page=";
LazyAdapter adapter;
public TextView textView;
ArrayList<HashMap<String, String>> jobsList = new ArrayList<HashMap<String, String>>();
MyTask mt;
static final String KEY_ICON = "icon";
static final String KEY_TITLE = "title";
static final String KEY_OCCUPATION = "occupation";
static final String KEY_PUBLISHED = "published";
static final String KEY_URL = "url";
@Override
public void onCreate(Bundle savedInstanceState) {
StrictMode.enableDefaults();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button nextBtn = (Button) findViewById(R.id.nextBtn);
Button backBtn = (Button) findViewById(R.id.backBtn);
textView = (TextView)findViewById(R.id.textView1);
textView.setText("Страница: "+String.valueOf(pageNumber));
showJobList();
nextBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d("NEXT", "CLICKED!!!");
pageNumber++;
showJobList();
}
});
backBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d("BACK", "CLICKED!!!");
if(pageNumber <= 1) return;
else pageNumber--;
showJobList();
}
});
}
public void showJobList() {
if(!hasInternetConnection()) {
Toast toast = Toast.makeText(getApplicationContext(), "no internet connection", Toast.LENGTH_LONG);
toast.show();
return;
}
TextView textView = (TextView)findViewById(R.id.textView1);
ListView listView = (ListView) findViewById(R.id.listView1);
jobsList.clear();
textView.setText("Страница: "+String.valueOf(pageNumber));
String readJobs = readJOBS();
mt = new MyTask();
mt.execute(readJobs);
try {
jobsList = mt.get();
} catch(Exception e) {
}
adapter=new LazyAdapter(this, jobsList);
listView.setAdapter(adapter);
// Click event for single list row
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.d("Item", jobsList.get((int)arg3).get("url"));
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("url", jobsList.get((int)arg3).get("url"));
startActivity(intent);
Log.d("Item", "CLICKED!!!");
}
});
}
public static Bitmap DownloadImage(String URL) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bitmap;
}
private static InputStream OpenHttpConnection(String urlString) throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (Exception ex) {
throw new IOException("Error connecting");
}
return in;
}
public String readJOBS() {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url + pageNumber);
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode ==200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
}
else {
Log.e(MainActivity.class.toString(), "Download fail");
}
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}
public boolean hasInternetConnection() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
class MyTask extends AsyncTask<String, Void, ArrayList<HashMap<String, String>>> {
@Override
protected ArrayList<HashMap<String, String>> doInBackground(String... params) {
try {
JSONObject jsonObject = new JSONObject(params[0]);
JSONArray jobs = jsonObject.getJSONArray("jobs");
for (int i = 0; i < jobs.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject jobsObj1 = jobs.getJSONObject(i);
String str1 = String.valueOf(jobsObj1.getString(KEY_TITLE));
String str2 = "Опубликовано: "+String.valueOf(jobsObj1.getString(KEY_PUBLISHED));
String str3 = "Занятость: "+String.valueOf(jobsObj1.getString(KEY_OCCUPATION));
String str4 = String.valueOf(jobsObj1.getString(KEY_ICON));
String str5 = String.valueOf(jobsObj1.getString(KEY_URL));
map.put(KEY_TITLE, str1);
map.put(KEY_OCCUPATION, str3);
map.put(KEY_PUBLISHED, str2);
map.put(KEY_ICON, str4);
map.put(KEY_URL, str5);
jobsList.add(map);
}
} catch (Exception e) {
e.printStackTrace();
Log.d("async task", "fail");
}
return jobsList;
}
}
}
он загружает с сайта вакансии. делает все это в асинхронном потоке, но не так как хотелось бы. нужно чтоб он выводил строки постепенно, пока остальные подгружаются. догадываюсь что нужно использовать методы onProgressUpdate и publishProgress но как это правильно делать никак не могу понять