【发布时间】:2014-04-29 12:50:04
【问题描述】:
我按照这里的教程:http://www.tutecentral.com/restful-api-for-android-part-1/ 使用 ASP.NET 创建了一个 RESTful API,一切都很好。在教程结束时,我下载了一个 ZIP 文件,其中包含一个 Java 文件,其中调用了 API 中的方法。
问题是...如何调用这些方法以便它们与我的 Web 服务 API 交互?我真的很困惑,我过去在我的 Web 服务器上使用 PHP 并插入/提取 SQL 数据做过类似的事情,但从来没有使用 ASP.NET 或类似的设置。
如果有帮助,以下是我在该教程结束时获得的 Java 文件的内容:
public class RestAPI {
private final String urlString = "http://localhost:53749/Gen_Handler.ashx";
private static String convertStreamToUTF8String(InputStream stream) throws IOException {
String result = "";
StringBuilder sb = new StringBuilder();
try {
InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[4096];
int readChars = 0;
while (readChars != -1) {
readChars = reader.read(buffer);
if (readChars > 0)
sb.append(buffer, 0, readChars);
}
result = sb.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return result;
}
private String load(String contents) throws IOException {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(60000);
conn.setDoOutput(true);
conn.setDoInput(true);
OutputStreamWriter w = new OutputStreamWriter(conn.getOutputStream());
w.write(contents);
w.flush();
InputStream istream = conn.getInputStream();
String result = convertStreamToUTF8String(istream);
return result;
}
private Object mapObject(Object o) {
Object finalValue = null;
if (o.getClass() == String.class) {
finalValue = o;
}
else if (Number.class.isInstance(o)) {
finalValue = String.valueOf(o);
} else if (Date.class.isInstance(o)) {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss", new Locale("en", "USA"));
finalValue = sdf.format((Date)o);
}
else if (Collection.class.isInstance(o)) {
Collection<?> col = (Collection<?>) o;
JSONArray jarray = new JSONArray();
for (Object item : col) {
jarray.put(mapObject(item));
}
finalValue = jarray;
} else {
Map<String, Object> map = new HashMap<String, Object>();
Method[] methods = o.getClass().getMethods();
for (Method method : methods) {
if (method.getDeclaringClass() == o.getClass()
&& method.getModifiers() == Modifier.PUBLIC
&& method.getName().startsWith("get")) {
String key = method.getName().substring(3);
try {
Object obj = method.invoke(o, null);
Object value = mapObject(obj);
map.put(key, value);
finalValue = new JSONObject(map);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
return finalValue;
}
public JSONObject CreateNewAccount(String firstName,String lastName,String userName,String password) throws Exception {
JSONObject result = null;
JSONObject o = new JSONObject();
JSONObject p = new JSONObject();
o.put("interface","RestAPI");
o.put("method", "CreateNewAccount");
p.put("firstName",mapObject(firstName));
p.put("lastName",mapObject(lastName));
p.put("userName",mapObject(userName));
p.put("password",mapObject(password));
o.put("parameters", p);
String s = o.toString();
String r = load(s);
result = new JSONObject(r);
return result;
}
public JSONObject GetUserDetails(String userName) throws Exception {
JSONObject result = null;
JSONObject o = new JSONObject();
JSONObject p = new JSONObject();
o.put("interface","RestAPI");
o.put("method", "GetUserDetails");
p.put("userName",mapObject(userName));
o.put("parameters", p);
String s = o.toString();
String r = load(s);
result = new JSONObject(r);
return result;
}
public JSONObject UserAuthentication(String userName,String passsword) throws Exception {
JSONObject result = null;
JSONObject o = new JSONObject();
JSONObject p = new JSONObject();
o.put("interface","RestAPI");
o.put("method", "UserAuthentication");
p.put("userName",mapObject(userName));
p.put("passsword",mapObject(passsword));
o.put("parameters", p);
String s = o.toString();
String r = load(s);
result = new JSONObject(r);
return result;
}
public JSONObject GetDepartmentDetails() throws Exception {
JSONObject result = null;
JSONObject o = new JSONObject();
JSONObject p = new JSONObject();
o.put("interface","RestAPI");
o.put("method", "GetDepartmentDetails");
o.put("parameters", p);
String s = o.toString();
String r = load(s);
result = new JSONObject(r);
return result;
}
}
在我的活动/片段中,我尝试这样做,但没有返回任何内容:(。
public void displaySafeMessage(View view) {
RestAPI test = new RestAPI();
JSONObject jsonObj = new JSONObject();
try {
//jsonObj = test.CreateNewAccount("tudor","hofnar","tudor.hofnar","password");
jsonObj = test.GetDepartmentDetails();
} catch (Exception e) {
}
String tmp = jsonObj.toString();
Toast.makeText(getActivity(), tmp, Toast.LENGTH_LONG).show();
}
注意:displaySafeMessage() 方法是从我没有包含的按钮 onClick() 调用的。此代码在 Android 中没有错误,但也没有返回任何内容,并且我在 Departments 表中有值,作为SQL 查询显示。
我知道我在这里遗漏了一件重要的事情,所以请告诉我它是什么! 谢谢!
【问题讨论】: