Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
595 views
in Technique[技术] by (71.8m points)

httpclient - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...