我在 asp.net 中创建了一个 Web 服务,它接收以下 url:
http://localhost:31804/api/defaultapi/login?empNum=123456&surname=Yusuf
并发送以下 json {"$id":"1","EmployeeID":2,"Firstname":"Abayomi","Surname":"Yusuf","DepartmentID":1,"EmployeeNumber":"123456" }
这适用于我的浏览器。
现在我正在尝试在我的 android 应用程序中使用该 Web 服务。我是一个初学者,我在这里使用 Andrew Barber 创建的 JSON 解析器类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;
}
}
这就是我在代码中使用它的方式。按钮具有 loadHome 方法作为其 onClick 事件
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");
}
}
}
我在 displayError textView 中不断收到错误消息(“哎呀 - 出了点问题!”)。我究竟做错了什么。
这是堆栈跟踪的链接 http://3.bp.blogspot.com/-jJnZ71KC5ZM/UUjVQhbzKCI/AAAAAAAAAMk/v9j_bhIkOEg/s1600/1.jpg