Java Android应用程序中的RESTAPI问题
这是我的RESTAPI,我无法将任何数据设置到MSSQL服务器。我有以下错误:Java Android应用程序中的RESTAPI问题,java,android,Java,Android,这是我的RESTAPI,我无法将任何数据设置到MSSQL服务器。我有以下错误: Unable to resolve host "bmsoft.somee.com": No address associated with hostname 这是我的RestAPI课程: public class RestAPI { private final String urlString = "http://bmsoft.somee.com/Handler1.ashx"; private
Unable to resolve host "bmsoft.somee.com": No address associated with hostname
这是我的RestAPI课程:
public class RestAPI {
private final String urlString = "http://bmsoft.somee.com/Handler1.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 readedChars = 0;
while (readedChars != -1) {
readedChars = reader.read(buffer);
if (readedChars > 0)
sb.append(buffer, 0, readedChars);
}
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 SetTA(double tj,double aj,String code) throws Exception {
JSONObject result = null;
JSONObject o = new JSONObject();
JSONObject p = new JSONObject();
o.put("interface","RestAPI");
o.put("method", "SetTA");
p.put("tj",mapObject(tj));
p.put("aj",mapObject(aj));
p.put("code",mapObject(code));
o.put("parameters", p);
String s = o.toString();
String r = load(s);
result = new JSONObject(r);
return result;
}
}
公共类RestAPI{
私有最终字符串urlString=”http://bmsoft.somee.com/Handler1.ashx";
私有静态字符串convertStreamToUTF8String(InputStream流)引发IOException{
字符串结果=”;
StringBuilder sb=新的StringBuilder();
试一试{
InputStreamReader reader=新的InputStreamReader(流,“UTF-8”);
char[]buffer=新字符[4096];
int readedChars=0;
while(readedChars!=-1){
readedChars=reader.read(缓冲区);
如果(读取字符>0)
sb.追加(缓冲区,0,readedChars);
}
结果=sb.toString();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
返回结果;
}
私有字符串加载(字符串内容)引发IOException{
URL=新URL(URL字符串);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod(“POST”);
连接设置连接超时(60000);
连接设置输出(真);
conn.setDoInput(真);
OutputStreamWriter w=新的OutputStreamWriter(conn.getOutputStream());
w、 写(内容);
w、 冲洗();
InputStream istream=conn.getInputStream();
字符串结果=convertStreamToUTF8String(istream);
返回结果;
}
私有对象映射对象(对象o){
对象finalValue=null;
if(o.getClass()==String.class){
最终价值=o;
}else if(编号、类别、实例(o)){
finalValue=String.valueOf(o);
}else if(日期、类别、持续时间(o)){
SimpleDateFormat sdf=新SimpleDateFormat(“MM/dd/yyyy hh:MM:ss”,新语言环境(“en”,“USA”);
最终值=sdf.格式((日期)o);
}else if(Collection.class.isInstance(o)){
集合col=(集合)o;
JSONArray jarray=新的JSONArray();
用于(对象项:col){
jarray.put(映射对象(项));
}
最终价值=jarray;
}否则{
Map Map=newhashmap();
方法[]方法=o.getClass().getMethods();
用于(方法:方法){
if(method.getDeclaringClass()==o.getClass()
&&方法.getModifiers()==Modifier.PUBLIC
&&方法.getName().startsWith(“get”)){
String key=method.getName().substring(3);
试一试{
Object obj=method.invoke(o,null);
对象值=映射对象(obj);
map.put(键、值);
最终值=新的JSONObject(映射);
}捕获(例外e){
e、 printStackTrace();
}
}
}
}
返回最终值;
}
公共JSONObject SetTA(双tj、双aj、字符串代码)引发异常{
JSONObject结果=null;
JSONObject o=新的JSONObject();
JSONObject p=新的JSONObject();
o、 put(“接口”、“重新启动PI”);
o、 put(“方法”、“设置”);
p、 put(“tj”,映射对象(tj));
p、 放置(“aj”,映射对象(aj));
p、 放置(“代码”,映射对象(代码));
o、 put(“参数”,p);
字符串s=o.toString();
字符串r=荷载;
结果=新的JSONObject(r);
返回结果;
}
}
您能告诉我们您收到了什么错误吗?您有“任何”错误吗?你的意思是你是否有错误?你的logcat怎么说?你是在后台线程上运行load()
吗?我有任何错误
意味着你有错误<代码>我没有任何错误表示您没有错误。那么,您是有错误还是没有错误?无法解析主机“bmsoft.somee.com”:没有与主机名关联的地址。您能告诉我们您收到了什么错误吗?您有“任何”错误吗?你的意思是你是否有错误?你的logcat怎么说?你是在后台线程上运行load()
吗?我有任何错误
意味着你有错误<代码>我没有任何错误表示您没有错误。那么您是否有错误,或者您没有错误?无法解析主机“bmsoft.somee.com”:没有与主机名关联的地址