-1

in my android project i need to retrieve some data from mysql through php .. i don't know if there is a better way than this, This is my code:

public class test extends AppCompatActivity {

TextView text;

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

    text = (TextView) findViewById(R.id.Name);

    AsyncTaskClass asyncTaskClass = new AsyncTaskClass();
    asyncTaskClass.execute();

}

private class AsyncTaskClass extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... strings) {
        String getNameURL = "http://10.0.2.2/getName.php";
        try {

            URL url = new URL(getNameURL);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

            try {

                httpURLConnection.setRequestMethod("GET");
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);

                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "iso-8859-1"));
                StringBuilder result = new StringBuilder();
                String line = "";

                while ((line = bufferedReader.readLine()) != null) {
                    result.append(line).append("\n");
                }

                bufferedReader.close();

                JSONObject jsonObject = new JSONObject(result.toString());
                return jsonObject.getString("Name");

            } finally {
                httpURLConnection.disconnect();
            }

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

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        if (result != null) {
            text.setText("Your name is " + result);
        }
    }
}}

there is nothing wrong with the code.

But note that in every activity I put an AsyncTask inside of it if it needs to get some data .. i don't have too much experience with android and java. is my HTTP connection have something wrong because i saw some examples using HTTPClient and URI instead of URL ! And what is the different between setRequsetmethod GET and POST ?

Thanks in advance I appreciate any help :) !

Fahd Al-Qahtani
  • 188
  • 1
  • 9

2 Answers2

0

I would suggest a networking library such as Android Volley. (There are also a number of others out there like Retrofit, and OkHttp2)

By using a networking library, it makes it much easier to reuse and read your code as well as a number of other perks like not having to use an AsyncTask.

Add this line to your project dependencies to import the library into your project

dependencies {
    ...
    compile 'com.mcxiaoke.volley:library:1.0.0'
}

There are many resources online that can show you how to use this library and the others I mentioned if you google something like "example android volley request."

user3331142
  • 1,222
  • 1
  • 11
  • 22
0

But note that in every activity I put an AsyncTask inside of it if it needs to get some data

So? Nothing wrong with that. It's your choice to organize your code better so you don't copy paste everything again and again.

is my HTTP connection have something wrong because i saw some examples using HTTPClient and URI instead of URL

Apache HTTPClient is deprecated on Android now.

what is the different between setRequsetmethod GET and POST

Nothing to do with Android... Do you want to GET data or "send" data? Like read from a database or insert into a database?

there is nothing wrong with the code.

Okay, great! But, you may want to consider other HTTP libraries on Android

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

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245