I use the following line of code to get HttpResponce
from the server. While running the code I get the following stack trace in the logcat. Any clue why?
Code used by me:
new DeleteDirectoryLoaderTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
class DeleteDirectoryLoaderTask extends AsyncTask<URL, Integer, Long>
{
DeleteDirectoryLoaderTask()
{
}
@Override
protected void onPreExecute()
{
}
@Override
protected Long doInBackground(URL... urls)
{
try
{
HttpResponse responceFromServer = m_HttpResponceInstance.makeRequest("post", HTTP_URI + "/" + "signup", registrationHeader, jsonMain);
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Integer... progress)
{
}
@Override
protected void onPostExecute(Long result)
{
}
}
public HttpResponse makeRequest(String httpMethod, String url, Map<String, String> Header, JSONObject jsonObject) throws Exception
{
HttpResponse responce = null;
if(httpMethod.equals("post"))
{
DefaultHttpClient httpclient = new DefaultHttpClient();
//url with the post data
HttpPost httpost = new HttpPost(url);
//passes the results to a string builder/entity
StringEntity se = new StringEntity(jsonObject.toString());
//sets the post request as the resulting string
httpost.setEntity(se);
for ( String key : Header.keySet() )
{
//sets a request header so the page receving the request
//will know what to do with it
httpost.setHeader(key, Header.get(key));
}
responce = httpclient.execute(httpost);
System.out.println(responce);
}
return responce;
}
Stack Trace in log cat
06-07 13:24:14.007: W/System.err(1857): android.os.NetworkOnMainThreadException
06-07 13:24:14.017: W/System.err(1857): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
06-07 13:24:14.022: W/System.err(1857): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
06-07 13:24:14.022: W/System.err(1857): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-07 13:24:14.022: W/System.err(1857): at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-07 13:24:14.027: W/System.err(1857): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
06-07 13:24:14.027: W/System.err(1857): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-07 13:24:14.027: W/System.err(1857): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-07 13:24:14.027: W/System.err(1857): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-07 13:24:14.032: W/System.err(1857): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
06-07 13:24:14.032: W/System.err(1857): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
06-07 13:24:14.032: W/System.err(1857): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-07 13:24:14.032: W/System.err(1857): at in.plackal.lovecyclesfree.HttpResponceManager.makeRequest(HttpResponceManager.java:54)
06-07 13:24:14.037: W/System.err(1857): at in.plackal.lovecyclesfree.RegisterAccountActivity.onClick(RegisterAccountActivity.java:161)
06-07 13:24:14.037: W/System.err(1857): at android.view.View.performClick(View.java:4261)
06-07 13:24:14.037: W/System.err(1857): at android.view.View$PerformClick.run(View.java:17356)
06-07 13:24:14.037: W/System.err(1857): at android.os.Handler.handleCallback(Handler.java:615)
06-07 13:24:14.037: W/System.err(1857): at android.os.Handler.dispatchMessage(Handler.java:92)
06-07 13:24:14.042: W/System.err(1857): at android.os.Looper.loop(Looper.java:137)
06-07 13:24:14.042: W/System.err(1857): at android.app.ActivityThread.main(ActivityThread.java:4921)
06-07 13:24:14.042: W/System.err(1857): at java.lang.reflect.Method.invokeNative(Native Method)
06-07 13:24:14.042: W/System.err(1857): at java.lang.reflect.Method.invoke(Method.java:511)
06-07 13:24:14.047: W/System.err(1857): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
06-07 13:24:14.047: W/System.err(1857): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
06-07 13:24:14.047: W/System.err(1857): at dalvik.system.NativeStart.main(Native Method)