我正在开发一个简单的Android应用程序,需要获取并将数据发布到自行开发的web api。起初我让它像魅力一样工作,但在重新启动和更新我的iMac后,结果发现该应用程序不再像以前那样有效了。
在Android Studio中返回以下错误:java.net.UnknownHostException:无法解析主机" sensor.creatiefingewikkeld.com":没有与主机名关联的地址
这是我的SensorRestClient api类:
public class SensorRestClient {
private static final String BASE_URL = "http://sensor.creatiefingewikkeld.com/";
private static AsyncHttpClient client = new AsyncHttpClient();
public static void get(Context context, String url, Header[] headers, RequestParams params,
AsyncHttpResponseHandler responseHandler) {
Log.i("tester inside get",getAbsoluteUrl(url));
client.get(context, getAbsoluteUrl(url), headers, params, responseHandler);
}
public static void post(Context context, String url, RequestParams params,
AsyncHttpResponseHandler responseHandler){
client.post(getAbsoluteUrl(url), params, responseHandler);
}
private static String getAbsoluteUrl(String relativeUrl) {
return BASE_URL + relativeUrl;
}
}
这是我的MainActivity类:
public class MainActivity extends AppCompatActivity {
private ListView dataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getData();
//postData();
}
private void getData() {
List<Header> headers = new ArrayList<Header>();
headers.add(new BasicHeader("Accept", "application/json"));
SensorRestClient.get(MainActivity.this, "api/get", headers.toArray(new Header[headers.size()]),
null, new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
ArrayList<SensorData> dataArray = new ArrayList<SensorData>();
SensorDataAdapter dataAdapter = new SensorDataAdapter(MainActivity.this, dataArray);
Log.i("e","e");
for (int i = 0; i < response.length(); i++) {
try {
dataAdapter.add(new SensorData(response.getJSONObject(i)));
} catch (JSONException e) {
e.printStackTrace();
}
}
dataList = (ListView) findViewById(R.id.list_notes);
dataList.setAdapter(dataAdapter);
}
@Override
public void onFailure(int statusCode, Header[] headers, String res, Throwable t) {
Log.i("StatusCode:" , String.valueOf(statusCode) );
}
});
}
private void postData(){
RequestParams params = new RequestParams();
params.put("kind", "Temperature");
params.put("title", "Test from android");
params.put("data", "28");
SensorRestClient.post(MainActivity.this, "api/post", params,
new JsonHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
Log.i("Status-Posted: ", String.valueOf(statusCode));
}
}
);
}
}
我也在使用listView的适配器,但我认为现在不需要代码。
我希望有人可以帮我解决这个问题,因为。
答案 0 :(得分:0)
在浏览器中检查URI后,我很确定您只是遇到了(临时?)dns故障情况。你能否确认错误没有消失?