Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android HTTP Get

I've look at a few forum post and I can not find an answer to my problem. I am trying to get a response from a php file. The php file is working. The problem is the Android App will not execute my request. Here are two examples of my code and the result I get in the textview:

public void changText(View view) {
    TextView textv = (TextView)findViewById(R.id.textview1);
    textv.setText("Text Has Been Changed");
    BufferedReader in = null;
    String data = null;

    try{
           HttpClient httpclient = new DefaultHttpClient();

           HttpGet request = new HttpGet();
           URI website = new URI("http://alanhardin.comyr.com/matt24/matt28.php");
           request.setURI(website);
           HttpResponse response = httpclient.execute(request);
           in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

           textv.append(" Connected ");
       }catch(Exception e){
           Log.e("log_tag", "Error in http connection "+e.toString());
       }


   }

The TextView reads: Text Has Been Changed

    public void changText(View view) {
    TextView textv = (TextView)findViewById(R.id.textview1);
    textv.setText("Text Has Been Changed");
    BufferedReader in = null;
    String data = null;

    try{
           HttpClient httpclient = new DefaultHttpClient();

           HttpGet request = new HttpGet();
           URI website = new URI("http://alanhardin.comyr.com/matt24/matt28.php");
           request.setURI(website);
           //HttpResponse response = httpclient.execute(request);
           //in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

           textv.append(" Connected ");
       }catch(Exception e){
           Log.e("log_tag", "Error in http connection "+e.toString());
       }


   }

The TextView reads: Text Has Been Changed Connected

In this manifest I have:

<uses-permission android:name="android.permission.INTERNET" />

In the error log I get the following: Error in http connection android.os.NetworkOnMainThreadException

Any help would be appreciated.

like image 887
user908759 Avatar asked Dec 09 '22 11:12

user908759


1 Answers

Android is probably executing your request just fine. You just seem to be ignoring the data returned by the server. You could do something like this, for instance:

public void changText(View view) {
    TextView textv = (TextView)findViewById(R.id.textview1);
    textv.setText("Text Has Been Changed");
    BufferedReader in = null;
    String data = null;

    try{
           HttpClient httpclient = new DefaultHttpClient();

           HttpGet request = new HttpGet();
           URI website = new URI("http://alanhardin.comyr.com/matt24/matt28.php");
           request.setURI(website);
           HttpResponse response = httpclient.execute(request);
           in = new BufferedReader(new InputStreamReader(
                   response.getEntity().getContent()));

           // NEW CODE
           String line = in.readLine();
           textv.append(" First line: " + line);
           // END OF NEW CODE

           textv.append(" Connected ");
       }catch(Exception e){
           Log.e("log_tag", "Error in http connection "+e.toString());
       }
}

It looks like your server is returning a JSON object, so you would probably want to do something more intelligent such as read the entire response, parse it (using new JSONArray(response)), and extract relevant fields, but the above code would at least verify that Android is executing your query.

EDIT: From the exception you report, it appears that you are running your code on the main UI thread. As of API 11 that is prohibited (and was frowned upon before that). There are several articles on how to fix this. See the Guide topic Painless threading as well as tutorials here and here. Also see this thread for more information.

like image 173
Ted Hopp Avatar answered Dec 23 '22 16:12

Ted Hopp