Android, веб-службы, AsyncTask, пожалуйста, помогите noob / убедитесь, что я начинаю правильно - PullRequest
0 голосов
/ 09 марта 2012

Я внес правки после публикации Я создаю приложение для Android, которое подключается к JSON API.Пока все работает нормально, за исключением случаев, когда есть задержка с сервера.Конечно, пользовательский интерфейс может перестать отвечать, если это займет слишком много времени.Я читал, что asynctask может решить мою проблему.Хотя у меня было действительно много времени с примерами.

Вот класс restclient, выполняющий вызовы http ... парсинг json и сохранение пользовательских объектов в общедоступном списке, к которому мой другой класс может получить доступ.

package com.bde.dgcr;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class RestClient {
static List<ResponseHolder> list = new ArrayList<ResponseHolder>();
protected Context context = this.context;

private static String convertStreamToString(InputStream is) {
    /*
       * To convert the InputStream to String we use the BufferedReader.readLine()
       * method. We iterate until the BufferedReader return null which means
       * there's no more data to read. Each line will appended to a StringBuilder
       * and returned as String.
       */
    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) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return sb.toString();
}


/* This is a test function which will connects to a given
  * rest service and prints it's response to Android Log with
  * labels "Praeda".
  */
public void  connect(String url) {


    AsyncTask<String, Void, Void> connection = new AsyncTask<String, Void, Void>() {
        protected Context context;
        @Override
        protected Void doInBackground(String... params) {
// Prepare a request object
            HttpGet httpget = new HttpGet(params[0]);

// Execute the request
            HttpResponse response;
            HttpClient httpclient = new DefaultHttpClient();
            try {
                list.clear();
                response = httpclient.execute(httpget);
                // Examine the response status
                Log.i("Praeda", response.getStatusLine().toString());

                // Get hold of the response entity
                HttpEntity entity = response.getEntity();
                // If the response does not enclose an entity, there is no need
                // to worry about connection release

                if (entity != null) {

                    // A Simple JSON Response Read
                    InputStream instream = entity.getContent();
                    String result = convertStreamToString(instream);

                    // A Simple JSONObject Creation
                    //JSONObject json= new JSONObject(result);
                    JSONArray jsonArray = new JSONArray(result);


                    // A Simple JSONObject Parsing
                    for (int i = 0; i < (jsonArray.length()); i++) {
                        JSONObject json_obj = jsonArray.getJSONObject(i);
                        ResponseHolder rh = new ResponseHolder(json_obj);
                        list.add(rh);
                    }


                    instream.close();

                }

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
             ListView listView1;


            super.onPostExecute(aVoid);    //To change body of overridden methods use       File | Settings | File Templates.
            ResponseHolderAdapter adapter = new ResponseHolderAdapter(context, R.layout.listview_item_row, RestClient.list);
            listView1 = (ListView) findViewById(R.id.listView1);

            View header = (View)         getLayoutInflater().inflate(R.layout.listview_header_row, null);
            listView1.addHeaderView(header);

            listView1.setAdapter(adapter);
        }
    };
    connection.execute(url);

}
}

Вот класс, вызывающий метод статического соединения и использующий список для перехода адаптера в представление списка.

public class JsonGrabber extends Activity {


private final String API_KEY = "key";
private final String SECRET = "secret";
private String state;
private String city;
private String country;
private static String mode;
private String md5;
private String url;
CourseSearch cs;
private ListView listView1;

/**
 * Called when the activity is first created.
 */


public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.results);
    Bundle extras = getIntent().getExtras();
    state = extras.getString("state");
    city = URLEncoder.encode(extras.getString("city"));
    country = extras.getString("country");
    mode = extras.getString("mode");
    md5 = MD5.getMD5(API_KEY + SECRET + mode);
    System.out.println(md5);
    url = "http://www.api.com/?key=" + API_KEY + "&mode=" + mode + "&id=1&sig=" + md5;
    String findByLocUrl = "http://www.api.com/?key=" + API_KEY + "&mode=" + mode + "&city=" + city + "&state=" + state + "&country=" + country + "&sig=" + md5;
    System.out.println(findByLocUrl);
    RestClient rc = new RestClient();
    rc.connect(findByLocUrl);
    //RestClient.connect(findByLocUrl);
/*        if (RestClient.list.isEmpty())
    {
        setContentView(R.layout.noresults);
    }     else
    {

    ResponseHolderAdapter adapter = new ResponseHolderAdapter(this, R.layout.listview_item_row, RestClient.list);
    listView1 = (ListView) findViewById(R.id.listView1);

    View header = (View) getLayoutInflater().inflate(R.layout.listview_header_row, null);
    listView1.addHeaderView(header);

    listView1.setAdapter(adapter);
    }

 */
}


}

Каким-то образом я должен объединить все это виспользуя внутренний класс, расширяющий asyctask, для выполнения вызовов API в фоновом режиме, проанализируйте добавление json в мой список и настройте адаптер.Я знаю, что у меня могут быть некоторые проблемы с ориентацией объекта, и прежде чем я продолжу работу над приложением, я надеялся, что вы, ребята, сможете убедиться, что он движется в правильном направлении.У меня есть несколько других классов, которые я не включил.Дайте мне знать, будет ли больше смысла, если я добавлю другие классы.Заранее благодарим за любую помощь, которую вы, парни / девушки, можете предложить.

Ответы [ 2 ]

1 голос
/ 09 марта 2012

Вы можете переписать свой метод подключения, используя AsyncTask, например:

public static void connect(String url) {

HttpClient httpclient = new DefaultHttpClient();

AsyncTask<String, Void, Void> connection = new AsyncTask<String, Void, Void>() {

@Override
protected Void doInBackground(String... params) {
// Prepare a request object
HttpGet httpget = new HttpGet(params[0]);

// Execute the request
HttpResponse response;
try {
    list.clear();

    response = httpclient.execute(httpget);
    // Examine the response status
    Log.i("Praeda", response.getStatusLine().toString());

    // Get hold of the response entity
    HttpEntity entity = response.getEntity();
    // If the response does not enclose an entity, there is no need
    // to worry about connection release

    if (entity != null) {

        // A Simple JSON Response Read
        InputStream instream = entity.getContent();
        String result= convertStreamToString(instream);

    // A Simple JSONObject Creation
    //JSONObject json= new JSONObject(result);
    JSONArray jsonArray = new JSONArray(result);


    // A Simple JSONObject Parsing
    for (int i = 0; i < (jsonArray.length()); i++) {
        JSONObject json_obj = jsonArray.getJSONObject(i);
        ResponseHolder rh = new ResponseHolder(json_obj);
        list.add(rh);
    }


     instream.close();
    }
    }
    } catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

connection.execute(url);
}
0 голосов
/ 09 марта 2012

Если вы хотите сделать это правильно, вы должны использовать контент-провайдера, службы и базы данных, вот хороший учебник:

http://mobile.tutsplus.com/tutorials/android/android-fundamentals-downloading-data-with-services/

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...