Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 来自url的AyncTask JSON_Android_Json_Url - Fatal编程技术网

Android 来自url的AyncTask JSON

Android 来自url的AyncTask JSON,android,json,url,Android,Json,Url,我的申请有一些问题,不确定出了什么问题。基本上,该程序将加载到我的android设备上,坐在那里什么也不做,控制台/logcat中不会显示错误消息,TextView上的文本也不会改变,只是想知道是否有人有任何见解,谢谢 11-03 21:55:21.474: I/System.out(16741): execute 11-03 21:55:21.474: I/System.out(16741): started 11-03 21:55:21.524: D/libEGL(16741): loade

我的申请有一些问题,不确定出了什么问题。基本上,该程序将加载到我的android设备上,坐在那里什么也不做,控制台/logcat中不会显示错误消息,TextView上的文本也不会改变,只是想知道是否有人有任何见解,谢谢

11-03 21:55:21.474: I/System.out(16741): execute
11-03 21:55:21.474: I/System.out(16741): started
11-03 21:55:21.524: D/libEGL(16741): loaded /system/lib/egl/libEGL_tegra.so
11-03 21:55:21.544: D/libEGL(16741): loaded /system/lib/egl/libGLESv1_CM_tegra.so
11-03 21:55:21.554: D/libEGL(16741): loaded /system/lib/egl/libGLESv2_tegra.so
11-03 21:55:21.584: D/OpenGLRenderer(16741): Enabling debug mode 0
这是我的url中的JSON文件

{
"fruit": [
    {
        "type": "apple",
        "color": "green"
    },
    {
        "type": "orange",
        "color": "orange"
    },
    {
        "type": "banana",
        "color": "yellow"
    }
]
}

这是我第一次尝试为安卓设备编写代码,但不知道为什么什么都没有发生。当我从浏览器加载该设备时,该设备可以访问url,并且似乎加载良好

package com.example.fruitjson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

static String url = "http://192.168.0.14/fruitlist.json";
String TAG_FRUIT = "fruit";
String TAG_TYPE = "type";
String TAG_COLOR = "color";

JSONArray fruit = null;
JSONObject json = null;
InputStream is = null;
JSONObject result = null;
String jsonString = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    System.out.println("execute");
    new MyAsyncTask().execute();
    System.out.println("started");
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public class MyAsyncTask extends AsyncTask<String, Void, JSONObject> {
    @Override
    protected JSONObject doInBackground(String... arg0) {
        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            jsonString = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
        // try parse the string to a JSON object
        try {
            result = new JSONObject(jsonString);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }
        // return JSON String
        return result;
    }

    protected void onPostExecute(String result) {
        // get text views, loop out strings
        System.out.println("getting text views");
        TextView t1 = (TextView) findViewById(R.id.textView1);
        TextView t2 = (TextView) findViewById(R.id.textView3);
        System.out.println(t1.getText());
        System.out.println(t2.getText());
        System.out.println("success");
        try {
            fruit = json.getJSONArray(TAG_FRUIT);
            for (int i = 0; i < fruit.length(); i++) {
                JSONObject jObj = fruit.getJSONObject(i);
                String type = jObj.getString(TAG_TYPE);
                String color = jObj.getString(TAG_COLOR);

                t1.setText(type);
                t2.setText(color);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

}
}
package com.example.json;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.io.UnsupportedEncodingException;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Menu;
导入android.widget.TextView;
公共类MainActivity扩展了活动{
静态字符串url=”http://192.168.0.14/fruitlist.json";
字符串TAG_FRUIT=“FRUIT”;
字符串标记\u TYPE=“TYPE”;
字符串标记\u COLOR=“COLOR”;
JSONArray-fruit=null;
JSONObject json=null;
InputStream=null;
JSONObject结果=null;
字符串jsonString=“”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println(“执行”);
新建MyAsyncTask().execute();
System.out.println(“已启动”);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
公共类MyAsyncTask扩展了AsyncTask{
@凌驾
受保护的JSONObject doInBackground(字符串…arg0){
//发出HTTP请求
试一试{
//defaultHttpClient
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
BufferedReader reader=新的BufferedReader(
新的InputStreamReader(is,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
jsonString=sb.toString();
}捕获(例外e){
Log.e(“缓冲区错误”,“错误转换结果”+e.toString());
}
//尝试将字符串解析为JSON对象
试一试{
结果=新的JSONObject(jsonString);
}捕获(JSONException e){
Log.e(“JSON解析器”,“错误解析数据”+e.toString());
}
//返回JSON字符串
返回结果;
}
受保护的void onPostExecute(字符串结果){
//获取文本视图,循环输出字符串
System.out.println(“获取文本视图”);
TextView t1=(TextView)findViewById(R.id.textView1);
TextView t2=(TextView)findViewById(R.id.textView3);
System.out.println(t1.getText());
System.out.println(t2.getText());
System.out.println(“成功”);
试一试{
fruit=json.getJSONArray(TAG_fruit);
for(int i=0;i
您没有使用正确的onPostExecute方法。您需要重写该方法:

@Override 
protected void onPostExecute(JSONObject result) { ... }

您是否检查了异常或试图在catch块中打印它们?这样做可以查看您遇到的问题