1

I'm tying to read data from server I'm using xampp) but the data is empty this is my connect activity:

public String link="";
public AsyncTaskConnect(String link){
    this.link=link;
}
@Override
protected Object doInBackground(Object[] params) {
    try{
        URL url=new URL(link);
        URLConnection connection=url.openConnection();
        BufferedReader reader=new BufferedReader(new InputStreamReader(connection.getInputStream()));
        StringBuilder builder=new StringBuilder();
        String line=null;
        while((line=reader.readLine())!=null){
            builder.append(line);
        }
        MainActivity.data=builder.toString();
    }catch (Exception e){
    }
    return "";
}

this is main activity:

public static String data="";
TextView txthello;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txthello=(TextView)findViewById(R.id.txthello);
    new AsyncTaskConnect("http://192.168.1.2/digikala/test.php").execute();
    txthello.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(MainActivity.this,data,Toast.LENGTH_LONG).show();
        }
    });
}

but it doesn't work, what should I do?

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
reza
  • 11
  • 2

2 Answers2

0

Using HttpURLConnection, it extends your URLConnection so I am changing just a little to your code. Given you have your query in the String variable link, this should work just as fine.

try {
                URL url = new URL(link);
                HttpURLConnection connection= (HttpURLConnection) url.openConnection();

                int responseCode = connection.getResponseCode();
                Log.i(TAG, "POST Response Code: " + responseCode);

                //Takes data only if response from WebService is OK
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    String inputLine;
                    StringBuilder response = new StringBuilder();

                    //Stores input line by line in response
                    while ((inputLine = in.readLine()) != null) {
                        response.append(inputLine);
                    }
                    in.close();
} catch (Exception e) {
            e.printStackTrace();
}

if you follow this snippet, response is the string which contains all of the response you get from your webservice, you can further convert it to JSON if you want.

Hope it works!

Ashvin Sharma
  • 563
  • 1
  • 5
  • 24
0

but the data is empty

Because execute is not a blocking call.

Assuming you can actually reach the server, MainActivity.data is an empty String until after the Asynctask onPostExecute

You can either use Volley, Okhttp, Retrofit, etc to simplify your networking code

Comparison of Android networking libraries: OkHTTP, Retrofit, and Volley

or add callbacks into your Asynctask

How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245