- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从 URL 解析 JSON 数据并将其显示在 listView 中,但出现此错误:
03-27 20:53:04.830 13960-14086/com.example.samplejson E/HttpHandler: IOException: Unable to resolve host "jsonplaceholder.typicode.com": No address associated with hostname
03-27 20:53:04.830 13960-14086/com.example.samplejson E/MainActivity: Response from url: null
03-27 20:53:04.830 13960-14086/com.example.samplejson E/MainActivity: Couldn't get json from server.
我已经在我的 androidmanifest.xml 中添加了这一行:
<uses-permission android:name="android.permission.INTERNET" />
这是我的 MainActivity.java 文件:
public class MainActivity extends AppCompatActivity {
private String TAG = MainActivity.class.getSimpleName();
private ProgressDialog pDialog;
private ListView lv;
// URL to get contacts JSON
private static String url = "https://jsonplaceholder.typicode.com/users";
ArrayList<HashMap<String, String>> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactList = new ArrayList<>();
lv = (ListView) findViewById(R.id.listview);
new GetContacts().execute();
}
/**
* Async task class to get json by making HTTP call
*/
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
// Getting JSON Array node
JSONArray contacts = new JSONArray(jsonStr);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString("id");
String name = c.getString("name");
String email = c.getString("email");
String username = c.getString("username");
String phone = c.getString("phone");
/* // Phone node is JSON Object
JSONObject phone = c.getJSONObject("phone");
String mobile = phone.getString("mobile");
String home = phone.getString("home");
String office = phone.getString("office");
*/
// tmp hash map for single contact
HashMap<String, String> contact = new HashMap<>();
// adding each child node to HashMap key => value
contact.put("id", id);
contact.put("name", name);
contact.put("email", email);
contact.put("phone", phone);
// adding contact to contact list
contactList.add(contact);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, contactList,
R.layout.item_list, new String[]{"nom", "email",
"tel"}, new int[]{R.id.name,
R.id.email, R.id.phone});
lv.setAdapter(adapter);
}
}
}
HttpHandler 类:
public class HttpHandler {
private static final String TAG = HttpHandler.class.getSimpleName();
public HttpHandler() {
}
public String makeServiceCall(String reqUrl) {
String response = null;
try {
URL url = new URL(reqUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
// read the response
InputStream in = new BufferedInputStream(conn.getInputStream());
response = convertStreamToString(in);
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException: " + e.getMessage());
} catch (ProtocolException e) {
Log.e(TAG, "ProtocolException: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "Exception: " + e.getMessage());
}
return response;
}
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
有什么建议吗?
最佳答案
问题可能是由于没有可用的互联网连接而发生的。
因此,只需在您的设备(运行应用程序的地方)连接到互联网后重新验证。如果已关闭,请将其打开。
希望对您有所帮助。
关于android - 尝试从 URL : Unable to resolve host "jsonplaceholder.typicode.com": No address associated with hostname 解析 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55385694/
您好,我正在测试一个使用 typicode/lowdb json 文件作为数据库的 express 应用程序,但我无法使用更新功能。这是一些代码示例: typicode/lowdb db.json {
我正在使用 husky 为 prettier 设置 git hook,但每当我尝试提交或推送时都会遇到此错误: > husky - Can't find npm in PATH. Skipping p
我是 AngularJS 的新手,所以我被这个小的Users应用程序困住了。 而不是在 Controller 内写入用户数据数组 - 就像我已经做过的 HERE (jsFiddle),成功 - 我想使
我有一个 react 项目,我尝试将一个 JSON 对象发布到我的 Typicode 模拟 json 服务器。 https://github.com/typicode/json-server#gett
我正在尝试从 URL 解析 JSON 数据并将其显示在 listView 中,但出现此错误: 03-27 20:53:04.830 13960-14086/com.example.samplejson
我是一名优秀的程序员,十分优秀!