1

Sorry I'm new in the world java and android. I would like to understand why it always responds error. Thank you!

try {
            HttpClient client = new DefaultHttpClient();  
            String getURL = "http://www.google.com";
            HttpGet get = new HttpGet(getURL);
            HttpResponse responseGet = client.execute(get);  
            HttpEntity resEntityGet = responseGet.getEntity();  
            if (resEntityGet != null) {  
            Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_LONG).show();
             }
    } catch (Exception e) {
        //e.printStackTrace();
        Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();
    }

Import

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

and yes and I entered permission internet

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.testhttp"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
Valkyrie00
  • 51
  • 1
  • 7
  • Use LogCat to examine the Java stack trace associated with your "error". If you do not understand the stack trace, edit your question and post it here. – CommonsWare Jul 14 '13 at 16:57
  • uncomment the `e.printStackTrace()` and see what is the exception in logcat – Navid777 Jul 14 '13 at 17:00
  • see http://stackoverflow.com/questions/22395417/error-strictmodeandroidblockguardpolicy-onnetwork – user4805969 Apr 19 '15 at 02:50

2 Answers2

2

seems to be like you are trying to make an http request in the UI thread.

put your request in another thread.

Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71
  • @user2068102 remove Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_LONG).show(); from try block as well as from catch block and use Log.d("OK","Ok"); at both try and catch block and see results in logCat ! – Tarsem Singh Jul 14 '13 at 17:44
  • if you want to use Toast than use it as : runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(.....).show(); } }); – Tarsem Singh Jul 14 '13 at 17:48
0

You can also use this code to make an HTTP Request:

public class RequestClient extends AsyncTask<String, Void, String>{
        Context context;

        public RequestClient(Context c) {
            context = c;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String... aurl){
        String responseString="";
        HttpClient client = null;
        try {
             client = new DefaultHttpClient();  
             HttpGet get = new HttpGet(LoginActivity.url);
             HttpResponse responseGet = client.execute(get);  
             HttpEntity resEntityGet = responseGet.getEntity();  
             if (resEntityGet != null) {  
                 responseString = EntityUtils.toString(resEntityGet);
                 Log.i("GET RESPONSE", responseString.trim());
             }
        } catch (Exception e) {
            Log.d("ANDRO_ASYNC_ERROR", "Error is "+e.toString());
        }
            Log.d("ANDRO_ASYNC_RESPONSE", responseString.trim());
            client.getConnectionManager().shutdown();
         return responseString.trim();

        }


        @Override
        protected void onPostExecute(String response) {
             super.onPostExecute(response); 
            }
    }
John Bensin
  • 301
  • 2
  • 5
  • 20