1

我是 BB 的新手,试图解析 Json 文件,只想在对话框中打印 Json 响应。但它引发了一个关于No Application Instance并且也在获取的错误IllegalStateException。我使用GETurl 方法。我还添加了UiApplication类似的权限..

UIApplicationScreen

public static void main(String[] args) {
    ApplicationPermissions permRequest = ApplicationPermissionsManager.getInstance().getApplicationPermissions();
    permRequest = new ApplicationPermissions();
    permRequest.addPermission( ApplicationPermissions.PERMISSION_INTERNET );
    ApplicationPermissionsManager.getInstance().invokePermissionsRequest( permRequest );

    UiFunApplication app = new UiFunApplication();
    app.enterEventDispatcher();

这是主屏幕代码....

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import net.rim.device.api.applicationcontrol.ApplicationPermissions;
import net.rim.device.api.applicationcontrol.ApplicationPermissionsManager;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;

import com.rim.samples.jsonme.cakeorder.org.json.me.JSONArray;
import com.rim.samples.jsonme.cakeorder.org.json.me.JSONObject;

public class UiMainscreen extends MainScreen  {

    public UiMainscreen() {
        Dialog.alert("asdasd");
         HttpConnection conn = null;
          InputStream in = null;
          ByteArrayOutputStream out = null;
          try {
           //  String url = "http://api.twitter.com/1/users/show.json?screen_name=Kaka";
             String url = "http://docs.blackberry.com/sampledata.json";

             conn = (HttpConnection) Connector.open(url, Connector.READ);
             conn.setRequestMethod(HttpConnection.GET);

             int code = conn.getResponseCode();
             if (code == HttpConnection.HTTP_OK) {
                in = conn.openInputStream();
                out = new ByteArrayOutputStream();
                byte[] buffer = new byte[in.available()];
                int len = 0;
                while ((len = in.read(buffer)) > 0) {
                   out.write(buffer);
                }
                out.flush();
                String response = new String(out.toByteArray());
                Dialog.alert("response is ::"+response);
                } catch (Exception e) {
             Dialog.alert(e.getMessage());
          } finally {
             if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Dialog.alert(e.getMessage());
                }
             }
             if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Dialog.alert(e.getMessage());
                }
             }
             if (conn != null) {
                try {
                    conn.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Dialog.alert(e.getMessage());
                }
             }
          }
        }
    }

更新::

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Vector;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

import net.rim.device.api.applicationcontrol.ApplicationPermissions;
import net.rim.device.api.applicationcontrol.ApplicationPermissionsManager;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.CheckboxField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;

import com.rim.samples.jsonme.cakeorder.org.json.me.JSONArray;
import com.rim.samples.jsonme.cakeorder.org.json.me.JSONObject;

public class UiMainscreen extends MainScreen implements FieldChangeListener {

    public UiMainscreen() {
        ButtonField loginButton;

        loginButton = new ButtonField("Go", ButtonField.CONSUME_CLICK);
        loginButton.setChangeListener(this);

        add(loginButton);

    }

    public void fieldChanged(Field field, int context) {
        // TODO Auto-generated method stub
        if (field instanceof ButtonField) {

            Dialog.alert("Message");
            HttpConnection conn = null;
            InputStream in = null;
            ByteArrayOutputStream out = null;
            try {
                // String url =
                // "http://api.twitter.com/1/users/show.json?screen_name=Kaka";
                String url = "http://docs.blackberry.com/sampledata.json";

                conn = (HttpConnection) Connector.open(url, Connector.READ);
                conn.setRequestMethod(HttpConnection.GET);

                int code = conn.getResponseCode();
                if (code == HttpConnection.HTTP_OK) {
                    in = conn.openInputStream();
                    out = new ByteArrayOutputStream();
                    byte[] buffer = new byte[in.available()];
                    int len = 0;
                    while ((len = in.read(buffer)) > 0) {
                        out.write(buffer);
                    }
                    out.flush();
                    String response = new String(out.toByteArray());
                    Dialog.alert("response is ::" + response);

                    /*
                     * JSONObject resObject = new JSONObject(response); String
                     * key = resObject.getString("vehicleType");
                     * 
                     * Vector resultsVector = new Vector(); JSONArray jsonArray
                     * = resObject.getJSONArray("name"); if (jsonArray.length()
                     * > 0) { for (int i = 0; i < jsonArray.length();i++) {
                     * Vector elementsVector = new Vector(); JSONObject jsonObj
                     * = jsonArray.getJSONObject(i);
                     * elementsVector.addElement(jsonObj
                     * .getString("experiencePoints"));
                     * elementsVector.addElement
                     * (jsonObj.getString("Insert Json Array Element Key2"));
                     * resultsVector.addElement(elementsVector); } }
                     */
                }
            } catch (Exception e) {
                Dialog.alert(e.getMessage());
            } finally {
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        Dialog.alert(e.getMessage());
                    }
                }
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        Dialog.alert(e.getMessage());
                    }
                }
                if (conn != null) {
                    try {
                        conn.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                        Dialog.alert(e.getMessage());
                    }
                }
            }
        }

    }
}
4

1 回答 1

2

试试这个代码 -

final ButtonField b=new ButtonField("JSON");
    add(b);

    FieldChangeListener listener=new FieldChangeListener() {
        public void fieldChanged(Field field, int context) {
            if(field==b){

                try {
                    String httpURL = "http://docs.blackberry.com/sampledata.json";  
                    HttpConnection httpConn;
                    httpConn = (HttpConnection) Connector.open(httpURL);
                    httpConn.setRequestMethod(HttpConnection.POST);
                    DataOutputStream _outStream = new DataOutputStream(httpConn.openDataOutputStream());
                    byte[] request_body = httpURL.getBytes();
                    for (int i = 0; i < request_body.length; i++) {
                    _outStream.writeByte(request_body[i]);
                    }
                    DataInputStream _inputStream = new DataInputStream(
                    httpConn.openInputStream());
                    StringBuffer _responseMessage = new StringBuffer();
                    int ch;
                    while ((ch = _inputStream.read()) != -1) {
                    _responseMessage.append((char) ch);
                    }
                    String res = (_responseMessage.toString());
                    String responce = res.trim();

                    Dialog.alert(responce);

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }

        }
    };
    b.setChangeListener(listener);
于 2012-07-25T07:05:47.947 回答