Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The key for me was to create a class called URLWithParams or something because AsyncTask will allow only 1 type to be sent IN, and I needed both the URL and the params for the HTTP request.</p> <pre><code>public class URLWithParams { public String url; public List&lt;NameValuePair&gt; nameValuePairs; public URLWithParams() { nameValuePairs = new ArrayList&lt;NameValuePair&gt;(); } } </code></pre> <p>and then I send it to a JSONClient:</p> <pre><code>public class JSONClient extends AsyncTask&lt;URLWithParams, Void, String&gt; { private final static String TAG = "JSONClient"; ProgressDialog progressDialog ; GetJSONListener getJSONListener; public JSONClient(GetJSONListener listener){ this.getJSONListener = listener; } @Override protected String doInBackground(URLWithParams... urls) { return connect(urls[0].url, urls[0].nameValuePairs); } public static String connect(String url, List&lt;NameValuePair&gt; pairs) { HttpClient httpclient = new DefaultHttpClient(); if(url == null) { Log.d(TAG, "want to connect, but url is null"); } else { Log.d(TAG, "starting connect with url " + url); } if(pairs == null) { Log.d(TAG, "want to connect, though pairs is null"); } else { Log.d(TAG, "starting connect with this many pairs: " + pairs.size()); for(NameValuePair dog : pairs) { Log.d(TAG, "example: " + dog.toString()); } } // Execute the request HttpResponse response; try { // Prepare a request object HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(pairs)); response = httpclient.execute(httpPost); // Examine the response status Log.i(TAG,response.getStatusLine().toString()); BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); String json = reader.readLine(); return json; } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } @Override protected void onPostExecute(String json ) { getJSONListener.onRemoteCallComplete(json); } public interface GetJSONListener { public void onRemoteCallComplete(String jsonFromNet); } } </code></pre> <p>Then call it from my main class like this</p> <pre><code>public class BookCatalog implements GetJSONListener { private final String TAG = this.getClass().getSimpleName(); private String catalog_url = "URL"; private void getCatalogFromServer() { URLWithParams mURLWithParams = new URLWithParams(); mURLWithParams.url = catalog_url; try { JSONClient asyncPoster = new JSONClient(this); asyncPoster.execute(mURLWithParams); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void onRemoteCallComplete(String jsonBookCatalogList) { Log.d(TAG, "received json catalog:"); Log.d(TAG, jsonBookCatalogList); JSONObject bookCatalogResult; try { bookCatalogResult = (JSONObject) new JSONTokener(jsonBookCatalogList).nextValue(); JSONArray books = bookCatalogResult.getJSONArray("books"); if(books != null) { ArrayList&lt;String&gt; newBookOrdering = new ArrayList&lt;String&gt;(); int num_books = books.length(); BookCatalogEntry temp; DebugLog.d(TAG, "apparently we found " + Integer.toString(num_books) + " books."); for(int book_id = 0; book_id &lt; num_books; book_id++) { JSONObject book = books.getJSONObject(book_id); String title = book.getString("title"); int version = book.getInt("price"); } } } catch (JSONException e) { e.printStackTrace(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload