Mrugesh
Mrugesh

Reputation: 4517

Http client gives Target host must not be null error

My code is as shown below:

public class YActivity extends AppCompatActivity {

@Override
    protected void onCreate(Bundle savedInstanceState) {
 try {
            Log.d(TAG, "onCreate: page response" + getResponseFromUrl("www.iammjet.in"));
        } catch (IOException e) {
            e.printStackTrace();
        }
}

public String getResponseFromUrl(String url) throws IOException {
        HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
        HttpGet httpget = new HttpGet(URL); // Set the action you want to do
        HttpResponse response = httpclient.execute(httpget); // Executeit
        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent(); // Create an InputStream with the response
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null)
            sb.append(line);

        String resString = sb.toString();

        is.close();
        return resString;
    }


}

Now what happens here is it gives me Target host must not be null, or set in parameters. scheme=null, host=null, path=www.iammjet.in error , what should I insert to make it work?

I am using compileSdkVersion 24. To support HttpClient I have added in my build.gradle as follows:

android {
    compileSdkVersion 24
    buildToolsVersion "25.0.1"
    useLibrary 'org.apache.http.legacy'
}

My error stack trace is as shown below:

java.lang.RuntimeException: Unable to start activity ComponentInfo{demand.inn.com.xyz.staging/demand.inn.com.xyz.activity.YActivity}: android.os.NetworkOnMainThreadException
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                                                    at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:135)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                                                 Caused by: android.os.NetworkOnMainThreadException
                                                                                    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
                                                                                    at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
                                                                                    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
                                                                                    at java.net.InetAddress.getAllByName(InetAddress.java:215)
                                                                                    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:142)
                                                                                    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
                                                                                    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
                                                                                    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)
                                                                                    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
                                                                                    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
                                                                                    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
                                                                                    at demand.inn.com.quflip.activity.YActivity.getResponseFromUrl(YActivity.java:328)
                                                                                    at demand.inn.com.quflip.activity.QuFlipActivity.onCreate(QuFlipActivity.java:94)
                                                                                    at android.app.Activity.performCreate(Activity.java:5990)
                                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                                    at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                    at android.os.Looper.loop(Looper.java:135) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 


                                                                                --------- beginning of crash

Upvotes: 0

Views: 969

Answers (2)

Philip Couling
Philip Couling

Reputation: 14873

There are three major problems with your code:

  1. Your URL is badly formatted (giving the error)
  2. You are doing networking on the main thread
  3. The client library you are trying to use is being removed from later versions of android. Use an URLConnection if you can.

Formatting your URL

Your browser lies to you. www.iammjet.in is not an URL. It's just a host name. To be an URL you MUST have a scheme, host and path (as the error tells you.

http://www.iammjet.in/ is valid. It has the scheme http, host www.iammjet.in and path /. Because you have given it just the hostname it's misinterpreted it to be just the path and said that the hostname is null.

Upvotes: 2

Adil Saiyad
Adil Saiyad

Reputation: 1592

pass "http://www.iammjet.in" instead of "www.iammjet.in"

Upvotes: 0

Related Questions