这是我从本地主机提取JSON数据的简单代码片段
public class JSONtest extends ActionBarActivity {
private ProgressDialog dialog;
String message3;
String message_short;
String[] items;
String[] short_items;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jsontest);
String localhost = "http://10.0.2.2/example/json/notification.php";
tv = (TextView) findViewById(R.id.textView1);
new ReadJSONFeedTask().execute(localhost);
}
private class ReadJSONFeedTask extends AsyncTask<String, Void, String>
{
protected void onPreExecute()
{
super.onPreExecute();
dialog = new ProgressDialog(JSONtest.this);
dialog.setMessage("Downloading Notifications. Please wait . . .");
dialog.setIndeterminate(false);
dialog.setCancelable(false);
dialog.show();
}
protected String doInBackground(String...urls)
{
return readJSONFeed(urls[0]);
}
protected void onPostExecute(String result)
{
dialog.dismiss();
try
{
JSONArray jsonArray = new JSONArray(result);
items = new String[jsonArray.length()];
short_items = new String[jsonArray.length()];
for(int i = 0; i < jsonArray.length(); i++)
{
JSONObject jobj = jsonArray.getJSONObject(i);
message3 = "PART 1 : " + jobj.getString("subject") + "\n\n" +
"PART 2 : " + jobj.getString("date");
items[i] = message3;
message_short = message_short + "\n\n" + message3;
}
}
catch(Exception e)
{
e.printStackTrace();
}
tv.setText(message_short);
}
}
public String readJSONFeed(String URL)
{
StringBuilder sb = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet hg = new HttpGet(URL);
try
{
HttpResponse response = client.execute(hg);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if(statusCode == 200)
{
HttpEntity en = response.getEntity();
InputStream content = en.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while((line = reader.readLine()) != null)
{
sb.append(line);
}
}
else
{
Log.e("JSON", "Failed to download File");
}
}
catch(ClientProtocolException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
return sb.toString();
}
}
我在同一个应用中的两个不同的活动中使用了完全相同的代码。唯一的区别是类的名称。当一个活动完美无缺时,另一个活动会发生错误,因为没有任何反应,TextView
会保持黑色。 Logcat for the Activity的日志不起作用是:
11-04 11:14:40.679: W/EGL_emulation(2925): eglSurfaceAttrib not implemented
11-04 11:14:40.749: W/System.err(2925): java.lang.NullPointerException
11-04 11:14:40.749: W/System.err(2925): at com.onclaveinc.ioexception.HomeScreen$ReadJSONFeedTask.onPostExecute(HomeScreen.java:129)
11-04 11:14:40.749: W/System.err(2925): at com.onclaveinc.ioexception.HomeScreen$ReadJSONFeedTask.onPostExecute(HomeScreen.java:1)
11-04 11:14:40.749: W/System.err(2925): at android.os.AsyncTask.finish(AsyncTask.java:632)
11-04 11:14:40.749: W/System.err(2925): at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-04 11:14:40.749: W/System.err(2925): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
11-04 11:14:40.749: W/System.err(2925): at android.os.Handler.dispatchMessage(Handler.java:102)
11-04 11:14:40.749: W/System.err(2925): at android.os.Looper.loop(Looper.java:136)
11-04 11:14:40.749: W/System.err(2925): at android.app.ActivityThread.main(ActivityThread.java:5017)
11-04 11:14:40.749: W/System.err(2925): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 11:14:40.749: W/System.err(2925): at java.lang.reflect.Method.invoke(Method.java:515)
11-04 11:14:40.749: W/System.err(2925): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-04 11:14:40.749: W/System.err(2925): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-04 11:14:40.769: W/System.err(2925): at dalvik.system.NativeStart.main(Native Method)
有人可以帮帮我吗?我真的需要他们两个工作。