1

i am using a mysql database in a host, i want recive a data from my host but when i get the data to a textview, it is in blank.

public class MainActivity extends Activity {
TextView tv1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv1=(TextView)findViewById(R.id.name);
    Loader loader = new Loader(getApplicationContext());
    tv1.setText(loader.loadInBackground());
}
public static class Loader extends AsyncTaskLoader<String>{
    public Loader (Context contexto)
    {
        super(contexto);

    }

    @Override
    public String loadInBackground() {

        String resultado = "";
        String entrada = "";

        DefaultHttpClient cliente = new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://comupunt.esy.es/cities.php");
        try {
            HttpResponse execute = cliente.execute(httpget);
            InputStream contenido = execute.getEntity().getContent();
            BufferedReader buffer = new BufferedReader(new InputStreamReader(contenido)); 

            String s="";
            while((s=buffer.readLine())!=null)
                resultado+=s;
            {

            }
        } catch (Exception e) {
            // TODO: handle exception
        }

        try {

            JSONObject object = new JSONObject(resultado);
            JSONArray jarray = object.getJSONArray("cities");

            for (int i=0;i<jarray.length();i++)
            {
                JSONObject jobject = jarray.getJSONObject(i);
                String name=jobject.getString("name");
                entrada += name;

            }


        } catch (Exception e) {
            // TODO: handle exception
            Log.e("WebService", e.getMessage());
        }

        return entrada;

    }
 }


 }

the php with json get this

{"cities":[{"name":"aitor"}]}

and the logcat:

12-02 04:24:01.492: E/WebService(2190): End of input at character 0 of 

thanks

2

1 Answer 1

1

you are in MainActivity so the line

HttpResponse execute = cliente.execute(httpget);

is Throwing networkonmainthreadexception you can check it by using (in first try catch as well)

catch (Exception e) {
        // TODO: handle exception
         Log.e("errormsg", e.ToString();
    }

so instead of using AsyncTaskLoader better to use AsyncTask.

public class MainActivity extends Activity {

TextView tv1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv1 = (TextView) findViewById(R.id.text);
    Exe exe = new Exe();
    try {
        URI uri = new URI("http://comupunt.esy.es/cities.php");
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    exe.execute();

}

class Exe extends AsyncTask<URL, String, String> {

    String entrada = "";

    @Override
    protected String doInBackground(URL... url) {

        String resultado = "";

        try {
            DefaultHttpClient cliente = new DefaultHttpClient();

            DefaultHttpClient httpClient = new DefaultHttpClient();
            URI uri = new URI("http://comupunt.esy.es/cities.php");
            HttpGet http = new HttpGet(uri);


            HttpResponse execute = cliente.execute(http);

            InputStream contenido = execute.getEntity().getContent();

            BufferedReader buffer = new BufferedReader(
                    new InputStreamReader(contenido));

            String s = "";

            while ((s = buffer.readLine()) != null) {
                resultado += s;
            }
            JSONObject object = new JSONObject(resultado);
            Log.e("WebService1", object.toString());
            JSONArray jarray = object.getJSONArray("cities");

            for (int i = 0; i < jarray.length(); i++) {
                JSONObject jobject = jarray.getJSONObject(i);
                String name = jobject.getString("name");
                entrada = name;

            }

        } catch (Exception e) {
            // TODO: handle exception
            Log.e("WebService", e.getMessage());
        }

        return entrada;

    }


    @Override
    protected void onPostExecute(String res) {

        tv1.setText(entrada);
    }
}

}

Sign up to request clarification or add additional context in comments.

2 Comments

thank you very much!! I have got do it!! but, I can fill an object of more data from the database right?
I can fill a listview with a differents data from a row by row? like this: name and age _______ name and age

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.