I am trying to fetch json data in android application using REST services of c#. it is throwing an error:
org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject
I have searched on SO in previous question but couldn't find any useful.
What I have tried is:-
In c#
public DataTable TestCheckEgrasUserLogin()
{
DataTable dt = new DataTable();
GenralFunction gf = new GenralFunction();
SqlParameter[] PM = new SqlParameter[2];
PM[0] = new SqlParameter("@UserName", SqlDbType.VarChar, 50) { Value = UserName };
PM[1] = new SqlParameter("@Password", SqlDbType.VarChar, 50) { Value = Password };
dt = gf.Filldatatablevalue(PM, "TestegAndroidUserLoginInfo", dt, null);
return dt;
}
I have tested it by running in visual studio, it is working fine and bringing data.
In android:-
public class MainActivity extends Activity {
private final static String SERVICE_URI = "`serviceUrl`";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
try
{
final EditText usernametxt = (EditText) findViewById(R.id.txtUser);
final EditText passwordtxt = (EditText) findViewById(R.id.txtPassword);
String username;
String Password;
username=usernametxt.getText().toString();
Password=passwordtxt.getText().toString();
if(username=="" || Password=="")
{
MessageBox("Please Enter UserName or Password");
}
else if (username.length()<1 || Password.length()<1)
{
MessageBox("Please Enter Credential Details");
}
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());
final HttpGet request = new HttpGet(SERVICE_URI+username+"/Password/"+Password);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type","application/json; charset=utf-8");
final DefaultHttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
final HttpResponse response = httpClient.execute(request);
final HttpEntity responseEntity = response.getEntity();
// Read response data into buffer
char[] buffer = new char[(int)responseEntity.getContentLength()];
String tmpStr10 = String.valueOf(buffer.length);
InputStream stream = responseEntity.getContent();
InputStreamReader reader = new InputStreamReader(stream);
int sizeOfJSONFile = stream.available();
reader.read(buffer);
stream.close();
JSONObject vehicle = new JSONObject(new String(buffer)); ** /////Error Comes here**
JSONArray plates=new JSONArray(vehicle.getString("CheckEgrasLoginResult"));
Bundle B=new Bundle();
String UserName= null;
String UserId =null;
Intent i = new Intent(getApplicationContext(), com.example.nic.newdemosecond.detailsact.class);
for (int j = 0; j < plates.length(); ++j) {
JSONObject Veh = new JSONObject(plates.getString(j));
UserName = Veh.getString("UserName");
UserId = Veh.getString("UserId");
}
i.putExtra("UserName", UserName);
i.putExtra("UserId", UserId);
startActivity(i);
}
catch (Exception e)
{
MessageBox("Invalid Login");
}
}
}) ;
}
error is coming at where I declared JsonObject
. As I am newbie on android, I am unable to catch this error.
Any help would be appreciated !