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.5k26 gold badges101 silver badges152 bronze badges
asked Jun 10, 2012 at 10:21

3 Answers 3

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 )

:)

answered Jun 10, 2012 at 10:45
Sign up to request clarification or add additional context in comments.

Comments

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
19.3k16 gold badges62 silver badges103 bronze badges
answered Jun 10, 2012 at 10:33

Comments

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.

answered Jun 10, 2012 at 11:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.