0

I am a newbie to android development so please be patient with me.

I am trying to post user id and password to a PHP page and return data (page working fine, tested and returns Json data).

I followed online guides and had similar problem to:

How to send data to a website using httpPost, app crashes

So I followed what was said in the following Answer within the above post https://stackoverflow.com/a/18588948/3415061

No more Errors , but now how do I control what happens after data returned and if valid how do I go to the next screen and display it?

The following function executes the request but how I do I get the response to the screen if valid or not.

@Override
protected Boolean doInBackground(Void... params) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.blag.com/blag.php");
    try {
         List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
        nameValuePairs.add(new BasicNameValuePair("acc", "blag"));
        nameValuePairs.add(new BasicNameValuePair("usr", "blag"));
        nameValuePairs.add(new BasicNameValuePair("pass", "blag"));

        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        return true;
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return false ;
}

Or am I suppose to access the response in here, but I tried to launch another screen from here but I got errors.

protected void onPostExecute(Boolean result) {
    super.onPostExecute(result);
    if(result){
        //successful request
    }else{
        //error in request response
    }
   // msgTextField.setText(""); // clear text box
}

Am I taking the correct approach to doing this?

Thanks in advance!!!

new code:

protected void onPostExecute(String result) {
    super.onPostExecute(result);
    if(result=="HEllO"){
        //successful request


            Intent dashboard = new Intent(getApplicationContext(), MainOrder.class);         
            dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(dashboard);

        //userFunction.logoutUser(getActivity().getApplicationContext());
    }else{
        //error in request response
    }
   // msgTextField.setText(""); // clear text box
}

error on GetApplicationContext: The method getApplicationContext() is undefined for the type NetRequestAsync

and error on line Command StartActivity The method startActivity(Intent) is undefined for the type NetRequestAsync

Community
  • 1
  • 1
yas
  • 3
  • 1
  • 4
  • now what do you exactly want? you want to parse that response? – M A. Mar 15 '14 at 16:00
  • I want to start a new screen (intent),if the response is good and pass the data across to the new screen. I have created the new screen but I am getting errors in code when trying to call the screen. here is the latest code – yas Mar 15 '14 at 16:58
  • protected void onPostExecute(String result) { super.onPostExecute(result); if(result=="HEllO"){ //successful request Intent dashboard = new Intent(getApplicationContext(), MainOrder.class); dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(dashboard); }else{ //error in request response } // msgTextField.setText(""); // clear text box } – yas Mar 15 '14 at 16:59
  • sorry that looks crap – yas Mar 15 '14 at 17:00
  • i added the new code to question above – yas Mar 15 '14 at 17:02

1 Answers1

0

If this code is inside an Activity of name, let's say MyCoolActivity, then you can access those methods with MyCoolActivity.this.getApplicationContext() and MyCoolActivity.this.startIntent()

Juan Sánchez
  • 980
  • 2
  • 10
  • 26
  • Sorry I dont understand, the intent code is inside a function called onPostExecute, which is inside a class called NetRequestAsync. I am trying to load a form from onPostExecute, but I have errors in code (red underline). the errors I have mentioned at the bottom of the question. – yas Mar 15 '14 at 17:20
  • Usually those classes like your `NetRequestAsync` class are contained inside an `Activity` class. If it is not your case, then you must pass the current `Activity` to this class as a parameter. Put the complete code of the file containing your class, so helping you will be easier. – Juan Sánchez Mar 15 '14 at 17:39
  • juan, thnx, that was the problem, I had the netrequestAsync in different class. like i said, i was reading online guide and i presumed i was suppose to create it in a new class. I moved class into mainactivity and its now showing the new screen. – yas Mar 15 '14 at 17:51