0

I have 2 problems. first the Toast get data from my PHP server in emulator but not in real phone with android 5. the app hasn't any crash but just Toast empty . second problem: when I run app in emulator and click the Button data get empty and for second click data received from server.

this is my simple php code:

<?php
    echo "hi";
?>

this is Asynctask Class:

package com.example.hamed.supermarketone;

import android.os.AsyncTask;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class AsyncTaskConnect extends AsyncTask {
    public String link;
    public AsyncTaskConnect(String link){
        this.link= link;
    }
    @Override
    protected Object doInBackground(Object[] params) {
        try {
            URL mUrl = new URL(link);
            URLConnection mConnecttion = mUrl.openConnection();
            BufferedReader mBuffReader = new BufferedReader(new InputStreamReader(mConnecttion.getInputStream()));
            StringBuilder mStringBuilder = new StringBuilder();
            String line = null;
            while ((line = mBuffReader.readLine())  !=null){
                mStringBuilder.append(line);
            }
            LoginPage.data=mStringBuilder.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}

and my Activity:

package com.example.hamed.supermarketone;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class LoginPage extends AppCompatActivity {

    public static String data="";
    LinearLayout mLinearLayout_gotoLogin;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {



        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_page);

        mLinearLayout_gotoLogin = (LinearLayout)findViewById(R.id.lnr_gotologin);
        mLinearLayout_gotoLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 new AsyncTaskConnect("http://nikayand.com/app/").execute();
                if (!data.equals("")){
                    Toast.makeText(G.context, data,Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}

Thanks for your help :-)

Hamed Keshavarz M
  • 476
  • 2
  • 13
  • 27

1 Answers1

2

You show your toast right after you execute your async task, so there will never be any data there, you have to show the toast in your onPostExecute method of your async task.

Also you should not be sending data to a static variable in your activity like this

public static String data="";

LoginPage.data=mStringBuilder.toString();

that's a bad way to access data

tyczj
  • 71,600
  • 54
  • 194
  • 296