Java Android Studio onPostexecute结果是一个;
您好,在Android Studio上有以下代码:Java Android Studio onPostexecute结果是一个;,java,android,arrays,json,android-studio,Java,Android,Arrays,Json,Android Studio,您好,在Android Studio上有以下代码: private class ConsultarrDatos extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... urls) { try { return downloadUrl
private class ConsultarrDatos extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
try {
return downloadUrl(urls[0]);
} catch (IOException e) {
return "Unable to retrieve web page. URL may be invalid.";
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
System.out.println("onPostExecute:::::::::::::::::::::::::: " + result);
String strJson= result;
String data = "";
try {
JSONObject jsonRootObject = new JSONObject(strJson);
JSONArray jsonArray = jsonRootObject.optJSONArray("Employee");
for(int i=0; i < jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
int id = Integer.parseInt(jsonObject.optString("id").toString());
String name = jsonObject.optString("name").toString();
float salary = Float.parseFloat(jsonObject.optString("salary").toString());
data += "Node"+i+" : \n id= "+ id +" \n Name= "+ name +" \n Salary= "+ salary +" \n ";
etName.setText(name);
}
} catch (JSONException e) {e.printStackTrace();}
}
}
私有类顾问扩展异步任务{
@凌驾
受保护的字符串doInBackground(字符串…URL){
试一试{
返回下载URL(URL[0]);
}捕获(IOE异常){
return“无法检索网页。URL可能无效。”;
}
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
System.out.println(“onPostExecute:;
字符串strJson=结果;
字符串数据=”;
试一试{
JSONObject jsonRootObject=新的JSONObject(strJson);
JSONArray JSONArray=jsonRootObject.optJSONArray(“员工”);
for(int i=0;i
问题是,当我从PHP文件接收数据时,println正在打印:
I/System.out:onPostExecute:
04-03 18:25:50.798 18046-18046/com.example.lorenzo.phpmysql I/System.out:���
我的php代码是好的,我没有任何错误或类似的事情
你知道为什么吗
非常感谢 这是我的下载URL
private String downloadUrl(String myurl) throws IOException {
Log.i("URL",""+myurl);
myurl = myurl.replace(" ","%20");
InputStream is = null;
// Only display the first 500 characters of the retrieved
// web page content.
int len = 500;
try {
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
int response = conn.getResponseCode();
Log.d("respuesta", "The response is: " + response);
is = conn.getInputStream();
// Convert the InputStream into a string
String contentAsString = readIt(is, len);
return contentAsString;
// Makes sure that the InputStream is closed after the app is
// finished using it.
} finally {
if (is != null) {
is.close();
}
}
}
public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
Reader reader = null;
reader = new InputStreamReader(stream, "UTF-8");
char[] buffer = new char[len];
reader.read(buffer);
return new String(buffer);
}
发布
下载URL(…)
的源代码,问题很可能就在那里。