mercredi 24 février 2016

AsyncTask

listview save and show when my Application Work offline mode .

how i can add here SQLite Database for insert the Json String with doInBackground then Select this data again from SQLite Database as Json Array String again with onPostExecute to view this josnArray to listview .

FetchDataTask.java

public class FetchDataTask extends AsyncTask<String, Void, String>{
private final FetchDataListener listener;
private String msg;

public FetchDataTask(FetchDataListener listener) {
    this.listener = listener;
}

@Override
protected String doInBackground(String... params) {
    if(params == null) return null;

    // get url from params
    String url = params[0];

    try {
        // create http connection
        HttpClient client = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);

        // connect
        HttpResponse response = client.execute(httpget);

        // get response
        HttpEntity entity = response.getEntity();

        if(entity == null) {
            msg = "No response from server";
            return null;        
        }

        // get response content and convert it to json string
        InputStream is = entity.getContent();
        return streamToString(is);
    }
    catch(IOException e){
        msg = "No Network Connection";
    }

    return null;
}

@Override
protected void onPostExecute(String sJson) {
    if(sJson == null) {
        if(listener != null) listener.onFetchFailure(msg);
        return;
    }        

    try {
        // convert json string to json array
        JSONArray aJson = new JSONArray(sJson);

        // create apps list
        List<Application> apps = new ArrayList<Application>();

        for(int i=0; i<aJson.length(); i++) {
            JSONObject json = aJson.getJSONObject(i);
            Application app = new Application();
            app.setnames(json.getString("names"));
            app.setTitle(json.getString("app_title"));
            app.setlat1(json.getString("app_lat1"));
            app.setlog1(json.getString("app_log1"));   
            app.setclientid(json.getString("app_clientid"));
            app.setempid(json.getString("empid"));
            app.setviscode(json.getString("app_viscode"));
            app.setclienttype(json.getString("app_clienttype"));
            app.setTel1(json.getString("app_Tel1"));
            app.setmobile1(json.getString("app_mobile1"));
            app.setaddr1(json.getString("app_addr1"));
            app.setccost(json.getString("app_ccost"));
            app.setTimeStamp(json.getString("date"));
            app.setVisitT(json.getString("VisitT"));
            app.setVisitR(json.getString("VisitR")); 
            app.setIcon(json.getString("icon"));

            // add the app to apps list
            apps.add(app);
        }

        //notify the activity that fetch data has been complete
        if(listener != null) listener.onFetchComplete(apps);
    } catch (JSONException e) {
        msg = "Invalid response";
        if(listener != null) listener.onFetchFailure(msg);
        return;
    }        
}

/**
 * This function will convert response stream into json string
 * @param is respons string
 * @return json string
 * @throws IOException
 */
public String streamToString(final InputStream is) throws IOException{
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder(); 
    String line = null;

    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } 
    catch (IOException e) {
        throw e;
    } 
    finally {           
        try {
            is.close();
        } 
        catch (IOException e) {
            throw e;
        }
    }

    return sb.toString();
}
}

Aucun commentaire:

Enregistrer un commentaire