2013-11-03 96 views
0

的AyncTask JSON在我的應用程序中遇到了一些問題,並且不確定哪裏出了問題。基本上,程序會加載到我的android設備上,只是坐在那裏什麼也不做,控制檯/ logcat中不會顯示任何錯誤消息,textviews上的文本也不會改變,只是想知道是否有人有任何見解。來自url

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

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

}

這是我第一次嘗試Android設備編寫我的JSON文件,而且不知道爲什麼什麼也沒有發生,則設備有權訪問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(); 
     } 

    } 

} 
} 
+0

具有u檢查異常或試圖打印他們catch塊做到這一點,看看其中u越來越萬阿英,蔣達清 –

回答

1

您沒有使用正確的onPostExecute方法。您需要覆蓋的方法:

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

這個工作三江源 – user2950720