Note that there are some explanatory texts on larger screens.

plurals
  1. PODefaultHttpClient's "connManager" is always null,cant execute http requests
    primarykey
    data
    text
    <p>i am building an application in which i prompt users to register. I have a <em>django-restful</em> server running at back end , and i m trying to make HTTP post requests to my server on android client with <em>DefaultHttpClient</em> class. I get email, username etc from user and at the button's onClick event, i create an <em>AsnycTask</em> to execute the request. Here is the code for the activity:</p> <pre><code>registerButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { String userName = usernameEditText.getText().toString(); String email = emailEditText.getText().toString(); String password = passwordEditText.getText().toString(); if( userName != null &amp;&amp; email != null &amp;&amp; password != null) { new RegisterEventHandler().execute(userName , email , password); } } }); .... class RegisterEventHandler extends AsyncTask&lt;String, Integer, Boolean&gt; { @Override protected Boolean doInBackground(String... params) { RequestHandler handler = new RequestHandler(); return handler.register(params[0], params[1], params[2]); } @Override protected void onPostExecute(Boolean result) { super.onPostExecute(result); if( result ) { AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); builder.setTitle(R.string.RegisterSuccessfullTitle); builder.setMessage(R.string.RegisterSuccessfullMessage); builder.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Intent redirect = new Intent(getApplicationContext() , SmartMapMainActivity.class); startActivity(redirect); } }); builder.create().show(); } else { AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext()); builder.setTitle(R.string.RegisterFailedTitle); builder.setMessage(R.string.RegisterFailedMessage); builder.create().show(); } } } </code></pre> <p>The RequestHandler class :</p> <pre><code>public class RequestHandler { public boolean register(String userName , String email , String password) { HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("http://127.0.0.1/users/"); try { JSONObject jsonObj = new JSONObject(); jsonObj.put("username", userName); jsonObj.put("email", email); jsonObj.put("password", password); StringEntity entity = new StringEntity(jsonObj.toString()); entity.setContentType("application/json"); httpPost.setEntity(entity); HttpResponse response = httpClient.execute(httpPost); if(response.getStatusLine().getStatusCode() == 200) return true; else return false; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } return false; } } </code></pre> <p>The problem is, <em>DefaultHttpClient</em>'s connManager(ClientConnectionManager) is always <strong>null</strong> and on the <em>execute()</em> method of the <em>HttpClient</em>, it always throws an <strong>IOException</strong>. I tried initializing the <em>DefaultHttpClient</em> in my activity , rather than in the AsyncTask's <em>doInBackground</em> method , but the result was the same. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload