I am trying to get the response code for the HttpReponse. I have changed the method for getting the response but it is not working.
Before I used this try & catch: (url is parameter for function)
try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost method = new HttpPost(url);
        if (params != null) {
            method.setEntity(new UrlEncodedFormEntity(params));
        }
        HttpResponse response = httpclient .execute(method);
        InputStream inputStream = response.getEntity().getContent();
        String result = convertInputStreamToString(inputStream);
        return result;
    }
    catch (ClientProtocolException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}
But this code gave me a runtime error in HttpResponse response = httpclient .execute(method);
So I changed my code:
public class RegisterActivity extends Activity {
String      username;
String      password;
InputStream is     = null;
String      result = null;
String      line   = null;
int         code;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);
    final EditText usernamefield = (EditText) findViewById(R.id.username_reg);
    final EditText passwordfield = (EditText) findViewById(R.id.password_reg);
    Button reg_btn = (Button) findViewById(R.id.reg_btn);
    reg_btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            username = usernamefield.getText().toString();
            password = passwordfield.getText().toString();
            insert();
            ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("username", usernamefield.getText().toString()));
            params.add(new BasicNameValuePair("password", passwordfield.getText().toString()));
            params.add(new BasicNameValuePair("action", "insert"));
        }
    });
}
public void insert()
{
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("username", username));
    nameValuePairs.add(new BasicNameValuePair("password", password));
    nameValuePairs.add(new BasicNameValuePair("action", "insert"));
    try
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://192.168.1.10/ferdos/service.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
        Log.e("pass 1", "connection success ");
    }
    catch (Exception e)
    {
        Log.e("Fail 1", e.toString());
        Toast.makeText(getApplicationContext(), "Invalid IP Address",
                Toast.LENGTH_LONG).show();
    }
    try
    {
        BufferedReader reader = new BufferedReader
                (new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
        Log.e("pass 2", "connection success ");
    }
    catch (Exception e)
    {
        Log.e("Fail 2", e.toString());
    }
    try
    {
        JSONObject json_data = new JSONObject(result);
        code = (json_data.getInt("code"));
        if (code == 1)
        {
            Toast.makeText(getBaseContext(), "Inserted Successfully",
                    Toast.LENGTH_SHORT).show();
        }
        else
        {
            Toast.makeText(getBaseContext(), "Sorry, Try Again",
                    Toast.LENGTH_LONG).show();
        }
    }
    catch (Exception e)
    {
        Log.e("Fail 3", e.toString());
    }
}}
Please help me with this code to solve my problem.
 
     
    