0

I've seen several tutorials for this, but they're all older than the Android update that prevents network calls in the main thread. I tried moving the code that called it to a RetrieveFeedTask, but that didn't help me, because I then had no way to get the data in my main thread. Here's what I did:

class RetrieveFeedTask extends AsyncTask <String, Void, String>
{

    private Exception exception;

    private String doInBackground()
    {
        String returnString = "sample";

         ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        //http post
        try{
             HttpClient httpclient = new DefaultHttpClient();
             HttpPost httppost = new HttpPost("http://localhost/sample_array.php");
             httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
             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);
               sb = new StringBuilder();
               sb.append(reader.readLine() + "\n");

               String line="0";
               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());
                }
        //paring data
        int step_num;
        String description;
        try{
              jArray = new JSONArray(result);
              JSONObject json_data=null;
              for(int i=0;i<jArray.length();i++){
                     json_data = jArray.getJSONObject(i);
                     step_num=json_data.getInt("step_num");
                     description=json_data.getString("description");
                 }
              }
              catch(JSONException e1){
                  Toast.makeText(getBaseContext(), "No step found" ,Toast.LENGTH_LONG).show();
              } catch (ParseException e1) {
                    e1.printStackTrace();
            }

        //System.out.println(result);
        return result;
    }

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        return null;
    }

}

Then in the main thread, I did this:

AsyncTask<String, Void, String> returnString = new RetrieveFeedTask().execute();

So how do I get that information to use in the main thread?

tereško
  • 58,060
  • 25
  • 98
  • 150
Fibericon
  • 5,684
  • 12
  • 37
  • 64

3 Answers3

3

You're almost ok with that but there are 2 errors:

  1. You're overloading the doInBackground method that could never get called.
  2. implement your stuff inside the "currently" empty doInBackground implementation.

If you want to get your data back on your main thread you can:

  1. use the runOnUiThread Method
  2. create an Anonymous Inner Class inside your activity that will extend your asynctask and overrides the onPostExecute method. ( which runs on the ui thread )

:)

Community
  • 1
  • 1
Andrea Baccega
  • 27,211
  • 13
  • 45
  • 46
1

You can make one web-service that returns data you need in JSON format. Put that on to your server. Now call that web-service using android. This way You can get data from PHP to android

keyser
  • 18,829
  • 16
  • 59
  • 101
Hardik
  • 1,429
  • 2
  • 19
  • 37
0

Implement your current code in empty doInBackground block

 @Override
protected String doInBackground(String... arg0) {
    // TODO Auto-generated method stub
    return null;
}

return string from above method, will be received in onPostExecute method, that you need to implement in your code too, which is actually missing at present.

Zoombie
  • 3,590
  • 5
  • 33
  • 40