wad wad - 2 months ago 18
HTTP Question

Android Webservices with php

i want create web service with php code , PHP file containing the text and I want to show this text android textVeiw

I connection with URL via openConnection() but i have error in class InputStreamReader

public class MainActivity extends AppCompatActivity {
TextView text;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

text = (TextView) findViewById(R.id.textView4);
}

public void onClick(View view) throws Exception {

URL hellofileurl = null;
try {
hellofileurl = new URL("http://waaddev.ahosti.net/web.php");
HttpURLConnection myfirstconnection = (HttpURLConnection) hellofileurl.openConnection();
InputStream stream = new InputStream(myfirstconnection.getInputStream());//i have error in this line
BufferedReader b = new BufferedReader(stream);
String hello = b.readLine();

} catch (Exception e) {
e.printStackTrace();
}
}
}

Answer

InputStream class has been deprecated.

You can use:

    URL url = new URL("http://waaddev.ahosti.net/web.php");
    URLConnection urlConnection = url.openConnection();
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

    String line;
    while ((line = bufferedReader.readLine()) != null)
    {
         content.append(line + "\n");
    }
    bufferedReader.close();
    String hello = content.toString();
Comments