2016-01-09 5 views
10

मैं wp-api प्लगइन का उपयोग कर अपनी WordPress वेबसाइट के लिए एंड्रॉइड ऐप बनाना चाहता हूं। मैं HttpRequest (GET) कैसे भेज सकता हूं और जेसन में प्रतिक्रिया कैसे प्राप्त कर सकता हूं?कैसे HttpRequest को भेजने और एंड्रॉइड में जेसन प्रतिक्रिया प्राप्त करने के लिए?

+1

http://developer.android.com/training/volley/index.html –

+0

आप इस http://stackoverflow.com/a/8655039 – user1140237

उत्तर

10

कोड के नीचे का प्रयास करें किसी URL से json पाने के लिए

HttpClient httpclient = new DefaultHttpClient(); 
HttpGet httpget= new HttpGet(URL); 

HttpResponse response = httpclient.execute(httpget); 

if(response.getStatusLine().getStatusCode()==200){ 
    String server_response = EntityUtils.toString(response.getEntity()); 
    Log.i("Server response", server_response); 
} else { 
    Log.i("Server response", "Failed to get server response"); 
} 
+3

HttpClient कहां से ढूंढें? मुझे कौन सा पैकेज शामिल करना है? – Tarion

+2

@ टारियन बस अपने ऐप लेवल build.gradle फ़ाइल में 'android' में 'useLibrary' org.apache.http.legacy'' जोड़ें 'डिफ़ॉल्ट कॉन्फिग' के ऊपर ब्लॉक करें। –

0
try { 
      String line, newjson = ""; 
      URL urls = new URL(url); 
      try (BufferedReader reader = new BufferedReader(new InputStreamReader(urls.openStream(), "UTF-8"))) { 
       while ((line = reader.readLine()) != null) { 
        newjson += line; 
        // System.out.println(line); 
       } 
       // System.out.println(newjson); 
       String json = newjson.toString(); 
       JSONObject jObj = new JSONObject(json); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
24

URL से JSON प्राप्त करने के लिए इस फ़ंक्शन का उपयोग करें।

public static JSONObject getJSONObjectFromURL(String urlString) throws IOException, JSONException { 
    HttpURLConnection urlConnection = null; 
    URL url = new URL(urlString); 
    urlConnection = (HttpURLConnection) url.openConnection(); 
    urlConnection.setRequestMethod("GET"); 
    urlConnection.setReadTimeout(10000 /* milliseconds */); 
    urlConnection.setConnectTimeout(15000 /* milliseconds */); 
    urlConnection.setDoOutput(true); 
    urlConnection.connect(); 

    BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream())); 
    StringBuilder sb = new StringBuilder(); 

    String line; 
    while ((line = br.readLine()) != null) { 
     sb.append(line + "\n"); 
    } 
    br.close(); 

    String jsonString = sb.toString(); 
    System.out.println("JSON: " + jsonString); 

    return new JSONObject(jsonString); 
} 

अपने प्रकट

<uses-permission android:name="android.permission.INTERNET" />

में इंटरनेट अनुमति जोड़ने के लिए तो फिर इस तरह इसका इस्तेमाल न भूलें:

try{ 
     JSONObject jsonObject = getJSONObjectFromURL(urlString); 
     // 
     // Parse your json here 
     // 
} catch (IOException e) { 
     e.printStackTrace(); 
} catch (JSONException e) { 
     e.printStackTrace(); 
} 
+2

अच्छा समाधान उपयोग कर सकते हैं, apache http क्लाइंट आयात करने की आवश्यकता नहीं है! – Jlange

+0

यहां कम से कम दो प्रमुख त्रुटियां। 1. 'urlConnection.setDoOutput (सत्य);' POST' विधि में परिवर्तन अनुरोध। 2. यह दो अनुरोधों को प्रभावी ढंग से निष्पादित करता है, 'नया इनपुटस्ट्रीम रीडर (url.openStream() 'एक बार फिर' urlConnection' और इसके सभी गुणों को अनदेखा करता है। 3.' sb.append (line + "\ n") ' एक अतिरिक्त 'स्ट्रिंग' बनाता है। –

4

मुझे लगता है कि वॉली उस के लिए सबसे अच्छी पसंद है।

इस post और इस post

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