强行关闭申请

时间:2012-04-04 04:25:48

标签: android json web-services httpclient

我正在尝试从Android应用程序访问jsp页面上的json数据“name”。我收到错误,应用程序被强制关闭。

我的jsp代码如下......

<%@page contentType="text/html; charset=UTF-8"%>
<%@page import="org.json.simple.JSONObject"%>
<%
JSONObject obj=new JSONObject();
obj.put("name","foo");
obj.put("num",new Integer(100));
obj.put("balance",new Double(1000.21));
obj.put("is_vip",new Boolean(true));
obj.put("nickname",null);
out.print(obj);
out.flush();
%>

我的android java代码如下......

package com.campuspro.start;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class JsonDemo extends Activity 
{
TextView tv=(TextView)findViewById(R.id.jsonresult);
HttpClient client=new DefaultHttpClient();
String url="http://10.0.2.2:7001/f/json.jsp";

@Override
protected void onCreate(Bundle savedInstanceState) {

    // TODO Auto-generated method stub
    try{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.result_json);
    HttpGet get=new HttpGet(url);
    HttpResponse r=client.execute(get);
    int status=r.getStatusLine().getStatusCode();
    if(status==200)
    {
        HttpEntity e=r.getEntity();
        String data=EntityUtils.toString(e);
        JSONArray arr=new JSONArray(data);
        JSONObject last=arr.getJSONObject(0);
        String result=last.getString("name");
        tv.setText(result);
    }

}catch(Exception e)
{

}
}

}

eclipse上的日志cat错误是......

04-04 09:30:53.910: D/AndroidRuntime(345): Shutting down VM
04-04 09:30:53.910: W/dalvikvm(345): threadid=1: thread exiting with uncaught exception           (group=0x4001d800)
04-04 09:30:54.190: E/AndroidRuntime(345): FATAL EXCEPTION: main
04-04 09:30:54.190: E/AndroidRuntime(345): java.lang.RuntimeException: Unable to   instantiate activity ComponentInfo{com.campuspro.start/com.campuspro.start.JsonDemo}:   java.lang.NullPointerException
04-04 09:30:54.190: E/AndroidRuntime(345):  at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
04-04 09:30:54.190: E/AndroidRuntime(345):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-04 09:30:54.190: E/AndroidRuntime(345):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-04 09:30:54.190: E/AndroidRuntime(345):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-04 09:30:54.190: E/AndroidRuntime(345):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 09:30:54.190: E/AndroidRuntime(345):  at android.os.Looper.loop(Looper.java:123)
04-04 09:30:54.190: E/AndroidRuntime(345):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-04 09:30:54.190: E/AndroidRuntime(345):  at java.lang.reflect.Method.invokeNative(Native Method)
04-04 09:30:54.190: E/AndroidRuntime(345):  at java.lang.reflect.Method.invoke(Method.java:521)
04-04 09:30:54.190: E/AndroidRuntime(345):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-04 09:30:54.190: E/AndroidRuntime(345):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-04 09:30:54.190: E/AndroidRuntime(345):  at dalvik.system.NativeStart.main(Native Method)
04-04 09:30:54.190: E/AndroidRuntime(345): Caused by: java.lang.NullPointerException
04-04 09:30:54.190: E/AndroidRuntime(345):  at android.app.Activity.findViewById(Activity.java:1637)
04-04 09:30:54.190: E/AndroidRuntime(345):  at com.campuspro.start.JsonDemo.<init>(JsonDemo.java:18)
04-04 09:30:54.190: E/AndroidRuntime(345):  at java.lang.Class.newInstanceImpl(Native Method)
04-04 09:30:54.190: E/AndroidRuntime(345):  at java.lang.Class.newInstance(Class.java:1429)
04-04 09:30:54.190: E/AndroidRuntime(345):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
04-04 09:30:54.190: E/AndroidRuntime(345):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
04-04 09:30:54.190: E/AndroidRuntime(345):  ... 11 more

1 个答案:

答案 0 :(得分:2)

中有错误
TextView tv=(TextView)findViewById(R.id.jsonresult);

您应该在setContentView(R.layout.result_json);

之后进行设置