2012-07-20 9 views
7

मेरे पास एक ऐसा एप्लिकेशन है जिसमें मैं एक PHP वेब सर्वर से एंड्रॉइड मोबाइल पर जेसन डेटा प्राप्त करना चाहता हूं। जो मेरे पास है वह एक यूआरएल है और यह मार रहा है कि यूआरएल मुझे
{"items":[{"latitude":"420","longitude":"421"}]} जैसे जेसन डेटा देता है। लेकिन मैं अपने एंड्रॉइड मोबाइल में इस जेसन प्रारूप को पुनः प्राप्त करना चाहता हूं और जेसन प्रारूप से अक्षांश और देशांतर के मान प्राप्त करना चाहता हूं।PHP सर्वर से एंड्रॉइड मोबाइल पर जेसन डेटा कैसे प्राप्त करें

हम इसे एंड्रॉइड मोबाइल पर कैसे प्राप्त कर सकते हैं ..?

अग्रिम धन्यवाद ..

+2

उपयोग httpclient एंड्रॉयड पर पर JSON प्रारूप में अपने तार के साथ डेटा विनिमय करने के सर्वर। अगर आप अटक जाते हैं, तो अपना कोड पोस्ट करें और हम मदद करने की कोशिश करेंगे। –

उत्तर

9

पहले नीचे मैं

JSONObject json = new JSONObject(jsonstring); 
JSONArray nameArray = json.names(); 
JSONArray valArray = json.toJSONArray(nameArray); 

JSONArray valArray1 = valArray.getJSONArray(1); 

valArray1.toString().replace("[", ""); 
valArray1.toString().replace("]", ""); 

int len = valArray1.length(); 

for (int i = 0; i < valArray1.length(); i++) { 

Country country = new Country(); 
JSONObject arr = valArray1.getJSONObject(i); 
country.setCountryCode(arr.getString("countryCode"));       
country.setCountryName(arr.getString("countryName")); 
arrCountries.add(country); 
} 




public static String convertinputStreamToString(InputStream ists) 
     throws IOException { 
    if (ists != null) { 
     StringBuilder sb = new StringBuilder(); 
     String line; 

     try { 
      BufferedReader r1 = new BufferedReader(new InputStreamReader(
        ists, "UTF-8")); 
      while ((line = r1.readLine()) != null) { 
       sb.append(line).append("\n"); 
      } 
     } finally { 
      ists.close(); 
     } 
     return sb.toString(); 
    } else { 
     return ""; 
    } 
} 
+0

यह 'convertinputStreamToString (है)' विधि क्या है? –

+0

मैंने उस विधि को जोड़ा है – Nirali

0

उपयोग कुछ की तरह देश विवरण लाने में कर रहा हूँ यूआरएल कनेक्शन

String parsedString = ""; 

    try { 

     URL url = new URL(yourURL); 
     URLConnection conn = url.openConnection(); 

     HttpURLConnection httpConn = (HttpURLConnection) conn; 
     httpConn.setAllowUserInteraction(false); 
     httpConn.setInstanceFollowRedirects(true); 
     httpConn.setRequestMethod("GET"); 
     httpConn.connect(); 

     InputStream is = httpConn.getInputStream(); 
     parsedString = convertinputStreamToString(is); 

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

JSON स्ट्रिंग कर

{ 
"result": "success", 
"countryCodeList": 
[ 
    {"countryCode":"00","countryName":"World Wide"}, 
    {"countryCode":"kr","countryName":"Korea"} 
] 
} 

यहाँ :

try { 
    HttpParams params = new BasicHttpParams(); 
    HttpConnectionParams.setSoTimeout(params, 0); 
    HttpClient httpClient = new DefaultHttpClient(params); 

    //prepare the HTTP GET call 
    HttpGet httpget = new HttpGet(urlString); 
    //get the response entity 
    HttpEntity entity = httpClient.execute(httpget).getEntity(); 

    if (entity != null) { 
     //get the response content as a string 
     String response = EntityUtils.toString(entity); 
     //consume the entity 
     entity.consumeContent(); 

     // When HttpClient instance is no longer needed, shut down the connection manager to ensure immediate deallocation of all system resources 
     httpClient.getConnectionManager().shutdown(); 

     //return the JSON response 
     JSONObject object = new JSONObject(response.trim()); 
     JSONArray jsonArray = object.getJSONArray("items"); 
     if(jsonArray != null) { 
      for(int i = 0 ; i < jsonArray.length() ; i++) { 
       JSONObject object1 = (JSONObject) jsonArray.get(i); 
       String latitude = object1.getString("latitude"); 
       String longitude = object1.getString("longitude"); 
      } 
     } 
    } 
}catch (Exception e) { 
    e.printStackTrace(); 
} 
+0

आपका कोड कैच ब्लॉक में जा रहा है ... ऐसा क्यों है ?? –

+0

यदि आप मुझे बताते हैं कि अपवाद क्या है, तो मैं मदद कर सकता हूं, यह कोड मेरे लिए काम कर रहा है, सिवाय इसके कि JSON भाग अलग है .. – Nermeen

+0

मुझे नहीं पता कि अपवाद क्या है लेकिन जब मैं पकड़ ब्लॉक में कुछ टोस्ट करता हूं यह टोस्ट हो जाता है लेकिन जब मैं अक्षांश और देशांतर को टोस्ट करता हूं तो यह टोस्ट नहीं करता है। –

1
String jsonStr = '{"menu": {' + 
     '"id": "file",' + 
     '"value": "File",' + 
     '"popup": {' + 
      '"menuitem": [' + 
      '{"value": "New", "onclick": "CreateNewDoc()"},' + 
      '{"value": "Open", "onclick": "OpenDoc()"},' + 
      '{"value": "Close", "onclick": "CloseDoc()"}' + 
      ']' + 
     '}' + 
     '}}'; 

JSON स्ट्रिंग http://json.org/example.html से वास्तव में है यही कारण है कि। यह सबसे अच्छा था जो मुझे इस उदाहरण के लिए मिल सकता था।

अब हमारे पास यह जगह है, JSONObject का उपयोग करना शुरू करें। आप नीचे दिए गए आयात की जरूरत है इस के लिए काम करने के लिए होगा: import org.json.JSONObject;

JSONObject jsonObj = new JSONObject(jsonStr); 
कि instantiated साथ

, हम JSON स्ट्रिंग से डेटा के विभिन्न टुकड़े को नहीं निकाला जा निम्न कर सकते हैं -

// grabbing the menu object 
JSONObject menu = jsonObj.getJSONObject("menu"); 


Reading =========> HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 
      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(); 
       result=sb.toString();=======>Here result is the json string 


// these 2 are strings 
String id = menu.getString("id"); 
String value = menu.getString("value"); 

// the popop is another JSON object 
JSONObject popup = menu.getJSONObject("popup"); 

// using JSONArray to grab the menuitems from under popop 
    JSONArray menuitemArr = popupObject.getJSONArray("menuitem"); 

// lets loop through the JSONArray and get all the items 
for (int i = 0; i < menuitemArr.length(); i++) { 
// printing the values to the logcat 
Log.v(menuitemArr.getJSONObject(i).getString("value").toString()); 
Log.v(menuitemArr.getJSONObject(i).getString("onclick").toString()); 
} 

एक आसान उदाहरण के लिए here

+0

मैं अपने प्रोग्राम में यह जेसन स्ट्रिंग कैसे प्राप्त करूं ?? –

+0

कि आप अपनी वापसी स्ट्रिंग को php में json प्रारूप –

+0

में परिवर्तित कर सकते हैं, मेरा php url json प्रारूप लौटा रहा है लेकिन बात यह है कि मैं इसे अपने प्रोग्राम में कैसे प्राप्त करूं? –

1

अपने Android ग्राहक से एक अनुरोध भेजें क्लिक

public static JSONObject getJSONFromHttpPost(String URL) { 

    try{ 
    // Create a new HttpClient and Post Header 
    DefaultHttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httpPost = new HttpPost(URL); 
    String resultString = null; 




     long t = System.currentTimeMillis(); 
     HttpResponse response = (HttpResponse) httpclient.execute(httpPost); 
     System.out.println("HTTPResponse received in [" + (System.currentTimeMillis()-t) + "ms]"); 
     // Get hold of the response entity (-> the data): 
     HttpEntity entity = response.getEntity(); 

     if (entity != null) { 
      // Read the content stream 
      InputStream instream = entity.getContent(); 

      // convert content stream to a String 
      resultString= convertStreamToString(instream); 
      instream.close(); 
      System.out.println("result String : " + resultString); 
      //resultString = resultString.substring(1,resultString.length()-1); // remove wrapping "[" and "]" 
      System.out.println("result String : " + resultString); 
      // Transform the String into a JSONObject 
      JSONObject jsonObjRecv = new JSONObject(resultString); 
      // Raw DEBUG output of our received JSON object: 
      System.out.println("<JSONObject>\n"+jsonObjRecv.toString()+"\n</JSONObject>"); 


      return jsonObjRecv; 
     } 
     }catch(Exception e){e.printStackTrace();} 


     return null; 

}

यहाँ

समारोह स्ट्रिंग

private static String convertStreamToString(InputStream is) { 

    BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder sb = new StringBuilder(); 

    String line =""; 
    try { 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    return sb.toString(); 
} 

अब तुम सब करने की है परिवर्तित करने के लिए है echo सर्वर

संबंधित मुद्दे