How to solve HTTP/1.1 401 error in below code.
static String deviceID="30000000",controllerDeviceID="null",partnerMode=null,phoneNumber="9578010375",phoneOS="ICS",phoneModel="MotoG",createdOn="124566",status="Active";
Details details;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HttpAsyncTask h=new HttpAsyncTask();
h.execute();
}
public static String POST(String url)
{
InputStream inputStream=null;
String json = "";
String result=null;
try {
HttpClient httpClient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost(url);
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("DeviceID",deviceID);
jsonObject.accumulate("ControllerPhoneNumber",controllerDeviceID);
jsonObject.accumulate("PhoneNumber",phoneNumber);
jsonObject.accumulate("DeviceOS",phoneOS);
jsonObject.accumulate("DeviceModel",phoneModel);
json=jsonObject.toString();
StringEntity se=new StringEntity(json);
httpPost.setEntity(se);
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse httpResponse = httpClient.execute(httpPost);
se.setContentEncoding(HTTP.UTF_8);
inputStream = httpResponse.getEntity().getContent();
Log.d("OUTPUT",httpResponse.getStatusLine().toString());
if (inputStream!=null)
result=convertInputStreamToString(inputStream);
else
result="Did not work";
}
catch (Exception e)
{
Log.v("InputStream",e.getLocalizedMessage());
}
return result;
}
private class HttpAsyncTask extends AsyncTask<String,Void,String>
{
@Override
protected String doInBackground(String... urls) {
POST(url);
return null;
}
@Override
protected void onPostExecute(String result)
{
Toast.makeText(getBaseContext(), "Data Sent!", Toast.LENGTH_LONG).show();
}
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException {
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}