android - android studio中的httpurl响应代码为空白
问题描述
我正在尝试从远程服务器 www.kudossoft.in/get_data030518.php 获取 mysql 数据。但是当我检查空白的 httpurl 响应代码时,我什么也没得到。我还在 mainifest 中添加了使用 Internet 的权限,我的代码如下,请参阅。我还检查了我的 php 文件权限,没问题。我还在同一个数据库中插入了一些来自另一个活动的数据,这些数据工作正常。
主要活动代码:
public class MainActivity extends AppCompatActivity {
ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
getJSON("http://kudossoft.in/get_data300518.php");
}
private void getJSON(final String urlWebService) {
class GetJSON extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
try {
loadIntoListView(s);
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(Void... voids) {
try {
URL url = new URL(urlWebService);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
StringBuilder sb = new StringBuilder();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String json;
while ((json = bufferedReader.readLine()) != null) {
sb.append(json + "\n");
}
return sb.toString().trim();
} catch (Exception e) {
return null;
}
}
}
GetJSON getJSON = new GetJSON();
getJSON.execute();
}
private void loadIntoListView(String json) throws JSONException {
JSONArray jsonArray = new JSONArray(json);
String[] heroes = new String[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject obj = jsonArray.getJSONObject(i);
heroes[i] = obj.getString("name");``
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, heroes);
listView.setAdapter(arrayAdapter);
}
}
解决方案
推荐阅读
- html - 如果我修改了边距,徽标如何保持在同一位置
- python - 如何在 python 3.8 中使用 pipenv 为现有 django 项目激活虚拟环境
- php - 即使凭据不正确,PHP 中的会话变量也允许我登录
- package - 使用 JuliaPro 安装 Julia 包时出现问题
- java - 在泛型方法的参数上使用迭代器
- sql - 从 JPA 执行 sql 查询的计数不匹配
- c++ - 即使片段 alpha 为 1.0,合成器也在混合 OpenGL
- r - 如何使用条件标签在循环中制作多个 ggplots
- html - Bootstrap CSS 库不会加载
- sql - Oracle SQL - 不同 ID 格式的 substr 匹配