I am having one private web-services in which i need to pass authentication and a JSONObject Request also.
The Authentication goes perfect and the Response is also OK but in response I am getting HTML Page Source code of the particular web-services implementation page.
Is their any problem with the JSONObject Request that i sent.
Please refer the code below:
public JSONObject getJSONFromUrl(String url)
{
try{
DefaultHttpClient httpClient=new DefaultHttpClient();
HttpPost httppost=new HttpPost(url);
httppost.setHeader("Authorization", "Basic "+Base64.encodeToString("abc:xyz".getBytes(), Base64.NO_WRAP));
//Posting request on htt
httppost.setHeader( "Content-Type", "application/json" );
httppost.setHeader("Accept","application/json");
JSONObject jsonPara = new JSONObject();
jsonPara.put("password", "abc");
jsonPara.put("username", "abc");
JSONObject jsonobj=new JSONObject();
jsonobj.put("request", "syncdata/get_country");
jsonobj.put("para",jsonPara);
Log.i("jason Object", jsonobj.toString());
StringEntity se2 = new StringEntity(jsonobj.toString());
se2.setContentEncoding("UTF-8");
se2.setContentType("application/json");
httppost.setEntity(se2);
HttpResponse httpResponse=httpClient.execute(httppost);
HttpEntity httpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}
catch(UnsupportedEncodingException ae)
{
ae.printStackTrace();
}
catch(ClientProtocolException ae)
{
ae.printStackTrace();
}
catch(IOException ae)
{
ae.printStackTrace();
}
catch(JSONException e)
{
e.printStackTrace();
}
try
{
BufferedReader reader=new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb=new StringBuilder();
String line=null;
while((line= reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
json =sb.toString();
}
catch(Exception ae)
{
ae.printStackTrace();
}
try
{
jObj=new JSONObject(json);
}
catch(JSONException e)
{
e.printStackTrace();
}
return jObj;
}
Also how can i confirm that the response that I am getting is JSON or some other format (other then debugging) and am i getting the correct response which i actually should get.
Refer this Code.
I hope it will helpful to you…
MainActivity.java
text.php