0

The below code is not working.

String url = "http://my/url/username/pswd";
String result = "";

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView tv;
    tv = (TextView) findViewById(R.id.text1);

    HttpClient httpclient = new DefaultHttpClient();  
    HttpGet request = new HttpGet(url);  
    request.addHeader("apikey", "DeveloperWy7ayxR");
    request.addHeader("Content-Type","application/json");

    ResponseHandler handler = new BasicResponseHandler();  
    try {  
        result = httpclient.execute(request, handler);  
    } catch (ClientProtocolException e) {  
        e.printStackTrace();  
    } catch (IOException e) {  
        e.printStackTrace();  
    }  

    httpclient.getConnectionManager().shutdown();  
    Log.i("myLog", result); 

    tv.setText(result);
}

I tested url and headers with advanced rest client, it worked. Am I missed anything?

gangadhars
  • 2,584
  • 7
  • 41
  • 68

1 Answers1

0

You should do networking operations in background.For e.g. run your code in AsyncTask

For more info see android.os.NetworkOnMainThreadException

and also ADD network permission in android manifest.xml file:

<uses-permission android:name="android.permission.INTERNET"/>
Community
  • 1
  • 1
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74