2012-03-03 7 views
5

Je développe l'application dans BlackBerry version 5.0, et j'avais importer toute la bibliothèque qui nécessite pour JSON dans 5.0.Json analyse dans Blackberry 5.0

i avait la bibliothèque de téléchargement de cette url http://supportforums.blackberry.com/t5/Java-Development/JSON-library/td-p/573687

même je ne reçois pas de réponse, ce que j'avais manquer dans ce code s'il vous plaît aidez-moi.

Voici mon code Pour l'analyse json.

package mypackage; 

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

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

import JSON_ME_Library.src.org.json.me.JSONArray; 
import JSON_ME_Library.src.org.json.me.JSONException; 
import JSON_ME_Library.src.org.json.me.JSONObject; 

import net.rim.device.api.ui.UiApplication; 
import net.rim.device.api.ui.component.ButtonField; 
import net.rim.device.api.ui.component.Dialog; 
import net.rim.device.api.ui.component.LabelField; 
import net.rim.device.api.ui.container.MainScreen; 


public final class MyScreen extends MainScreen 
{ 

    String url="http://www.appymail.com/iphone-messenger/456842/"; 

    public MyScreen() 
    {     
     setTitle("Json Parsing Sample"); 

     String aa=jsonresponse(url); 

     if(aa.equalsIgnoreCase("")) 
     { 
      add(new LabelField("NO res")); 
     } 
     else 
     { 
      parseJSONResponceInBB(aa); 

     } 


    } 



    void parseJSONResponceInBB(String jsonInStrFormat) 
    { 


     try { 
      JSONObject json = new JSONObject(jsonInStrFormat); 
      JSONArray jArray= json.getJSONArray("messages"); 

      //JSONArray arr=jArray.getJSONArray(0); 

      for(int i=0;i<jArray.length();i++) 
      { 
       JSONObject j = jArray.getJSONObject(i); 
       String from = j.getString("id");   
       add(new LabelField("id=="+from)); 

       String to =j.getString("title"); 
       add(new LabelField("title=="+to));   

       String message=j.getString("body");     
       add(new LabelField("Body=="+message)); 

      } 
     } catch (JSONException e) 
     {   
      e.printStackTrace(); 
     }  



    } 

    public static String jsonresponse (String url) 
    { 
     String response = null; 
     HttpConnection httpConnection = null; 
     InputStream inStream = null; 
     int code; 
     StringBuffer stringBuffer = new StringBuffer(); 

     try { 

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

      code = httpConnection.getResponseCode(); 

      if(code == HttpConnection.HTTP_OK) 
      { 
       inStream=httpConnection.openInputStream(); 
       int c; 


       while((c=inStream.read())!=-1) 
       { 
        stringBuffer.append((char)c); 
       } 
       response=stringBuffer.toString(); 
       System.out.println("Response Getting from Server is ================" + response); 



      } 
     else 
      { 
       UiApplication.getUiApplication().invokeLater(new Runnable() 
       { 

        public void run() 
        {  
         Dialog.inform("Connection error"); 
        } 
       }); 
      } 


     } 
     catch (Exception e) 
     { 

      System.out.println("caught exception in jsonResponse method"+e.getMessage()); 

     } 
     finally 
     { 

     //  if (outputStream != null) 
     //  { 
     //   outputStream.close(); 
     //  } 
       if (inStream != null) 
       { 
        try { 
         inStream.close(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
       if (httpConnection != null) 
       { 
        try { 
         httpConnection.close(); 
        } catch (IOException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 
     } 

     return response; 
    } 


} 
+0

Thank You! Votre code m'a aidé à comprendre mon problème :) – yanike

Répondre

1

Bonjour cher vous devez utiliser l'extension url pour BlackBerry

donc s'il vous plaît essayer de changer cette ligne

String aa=jsonresponse(url); 

comme

String aa=jsonresponse(url+";interface=wifi"); 

Une fois les données de téléchargement terminé avec succès à partir de l'URL puis une fois vérifier String aa obtenir une valeur ou non? si elle obtenir des données puis suivez

essayer si elle fonctionne bien puis passez par ce lien suivant

Guide for URL extensions

+0

laissez-moi savoir que cela fonctionne pour vous ou non? –

1

Entrez URL dans

 String url="Your url"; 
    String request=jsonresponse(url+";interface=wifi"); 
    String response = parseJSONResponceInBB(request); 
    if(response .equalsIgnoreCase("")) 
    { 
     add(new LabelField("NO res")); 
    } 
    else 
    { 
     add(new LabelField(response)); 
    } 
+0

Merci pour rply .. – Hasmukh