0

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

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"
    }
]

}

这是我第一次尝试为 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();
        }

    }

}
}
4

1 回答 1

1

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

@Override 
protected void onPostExecute(JSONObject result) { ... }
于 2013-11-03T23:50:56.423 回答