I created a web service in asp.net which is to receive the following url:
http://localhost:31804/api/defaultapi/login?empNum=123456&surname=Yusuf
and send the following json {"$id":"1","EmployeeID":2,"Firstname":"Abayomi","Surname":"Yusuf","DepartmentID":1,"EmployeeNumber":"123456"}
This works on my browser.
Now I am trying to consume that web service in my android app. I'm a total beginner and i am using the JSON parser class created by Andrew Barber here How to parse JSON in Android (last comment)
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException 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 e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
this is how i used it in my code. A button has loadHome method as its onClick event
public class Main extends Activity {
private SharedPreferences employeeDetails;
private static final String EMP_ID = "EmployeeID";
private static final String EMP_NAME = "Firstname";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void loadHome(View view)
{
EditText empNumEditText = (EditText)findViewById(R.id.employeeNumEditText);
EditText surnameEditText = (EditText)findViewById(R.id.surnameEditText);
TextView empNumError = (TextView)findViewById(R.id.empNumWarningTextView);
TextView surnameError = (TextView)findViewById(R.id.surnameWarningTextView);
TextView displayError = (TextView)findViewById(R.id.errorTextView);
String employeeNumber = empNumEditText.getText().toString();
String surname = surnameEditText.getText().toString();
//ensure that the form was completed
if((employeeNumber.length()>0) && (surname.length()>0))
{
try{
String encodedEmployeeNumber = URLEncoder.encode(employeeNumber, "UTF-8");
String encodedSurname = URLEncoder.encode(surname, "UTF-8");
String loginURL = "localhost:31804/api/defaultapi/login?empNum=" + encodedEmployeeNumber + "&surname=" + encodedSurname;
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(loginURL);
String empId = json.getString(EMP_ID);
String empSur = json.getString(EMP_NAME);
displayError.setText(empSur);
}
catch(Exception e){
displayError.setText("Whoops - something went wrong!");
e.printStackTrace();
}
}
else //display error messages
{
if (employeeNumber.length()>0){
empNumError.setText(" ");
}else{
empNumError.setText("Enter Your Employee Number");
}
if (surname.length()>0){
surnameError.setText(" ");
}else{
surnameError.setText("Enter Your Surname");
}
}
}
I keep getting the error ("Whoops - something went wrong!") in the displayError textView. What am i doing wrong.
Here is a link to the stack trace http://3.bp.blogspot.com/-jJnZ71KC5ZM/UUjVQhbzKCI/AAAAAAAAAMk/v9j_bhIkOEg/s1600/1.jpg