来自url的AyncTask JSON

时间:2013-11-03 21:59:19

标签: android json url

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

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

这是来自网址

的我的JSON文件
{
"fruit": [
    {
        "type": "apple",
        "color": "green"
    },
    {
        "type": "orange",
        "color": "orange"
    },
    {
        "type": "banana",
        "color": "yellow"
    }
]

}

这是我第一次尝试为Android设备编码并且不知道为什么没有发生任何事情,设备可以访问该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();
        }

    }

}
}

1 个答案:

答案 0 :(得分:1)

您没有使用正确的onPostExecute方法。您需要覆盖该方法:

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