2012-04-01 8 views
14

मैं एक HTTPS प्राप्त करें Google खरीदारी API लेकिन कुछ भी नहीं काफी मेरे लिए काम कर रहा है करने के लिए अनुरोध भेजने के लिए, उदाहरण के लिए यहाँ के लिए चाहते हैं कि मैं क्या पल में कोशिश कर रहा हूँ है:एंड्रॉयड - भेजा जा रहा है HTTPS प्राप्त अनुरोध

try {   
     HttpClient client = new DefaultHttpClient(); 
     HttpGet request = new HttpGet(); 
     request.setURI(new URI("https://www.googleapis.com/shopping/search/v1/public/products/?key={my_key}&country=&q=t-shirts&alt=json&rankByrelevancy=")); 
     HttpResponse response = client.execute(request); 
    } catch (URISyntaxException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return response; 
}  

अगर किसी को इस पर सुधार करने या इसे बदलने के बारे में कोई सुझाव है तो कृपया मुझे बताएं, अग्रिम धन्यवाद। अपने प्रकट

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

यही नहीं अपना पूरा कोड का उपयोग कर यह कोशिश कर सकते हैं वापसी कथन के दायरे में नहीं है। यानी आप इसे अपने प्रयास ब्लॉक में घोषित करते हैं ताकि बस काम न करे। क्या गलत हो रहा है? – Blundell

+0

मैंने कोशिश की ब्रैकेट को शामिल करने के लिए अपनी पोस्ट संपादित की है, लेकिन यह है। क्या मुझे कोशिश करने और पकड़ने वाले ब्रैकेट को हटा देना चाहिए और केवल 'अपवाद फेंकता है ...' का उपयोग करना चाहिए ताकि मैं प्रतिक्रिया चर का उपयोग कर सकूं? –

+2

क्या काम नहीं करता है? कोई अपवाद, Logcat? जब httpClient से समस्या आई, तो मुझे सलाह देने वाली पहली चीज़ हमेशा प्रतिक्रिया स्थिति कोड की जांच कर रही है यानी httpResponse.getStatusLine()। GetStatusCode(); – yorkw

उत्तर

38

आपको संकलन त्रुटि मिलनी चाहिए। इसलिए अब यदि आपको एक त्रुटि आपकी प्रतिक्रिया अशक्त के रूप में लौटा दी जाएगी है

HttpResponse response = null; 
try {   
     HttpClient client = new DefaultHttpClient(); 
     HttpGet request = new HttpGet(); 
     request.setURI(new URI("https://www.googleapis.com/shopping/search/v1/public/products/?key={my_key}&country=&q=t-shirts&alt=json&rankByrelevancy=")); 
     response = client.execute(request); 
    } catch (URISyntaxException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    return response; 
} 

:

यह सही संस्करण है।

एक बार जब आपके पास प्रतिक्रिया हो और इसे शून्य के लिए चेक किया जाए, तो आप सामग्री (यानी आपका JSON) प्राप्त करना चाहेंगे।

http://developer.android.com/reference/org/apache/http/HttpResponse.html http://developer.android.com/reference/org/apache/http/HttpEntity.html http://developer.android.com/reference/java/io/InputStream.html

response.getEntity().getContent(); 

यह आप के साथ काम करने के लिए एक InputStream देता है।

http://www.mkyong.com/java/how-to-convert-inputstream-to-string-in-java/

public static String convertStreamToString(InputStream inputStream) throws IOException { 
     if (inputStream != null) { 
      Writer writer = new StringWriter(); 

      char[] buffer = new char[1024]; 
      try { 
       Reader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"),1024); 
       int n; 
       while ((n = reader.read(buffer)) != -1) { 
        writer.write(buffer, 0, n); 
       } 
      } finally { 
       inputStream.close(); 
      } 
      return writer.toString(); 
     } else { 
      return ""; 
     } 
    } 

आप इसे से एक JSONObject बनाने की जरूरत आप इस स्ट्रिंग है जब:

http://developer.android.com/reference/org/json/JSONObject.html

आप नीचे दिए गए या समकक्ष करना चाहते हैं आप एक स्ट्रिंग में बदलने के लिए चाहते हैं
JSONObject json = new JSONObject(inputStreamAsString); 

हो गया!

+0

बेकार ढंग से काम किया, आप पर्याप्त धन्यवाद नहीं कर सकते! –

+0

मेरे लिए काम नहीं है? SSLPeerUnspicifiedException अवसर :( –

6

आप को यह जोड़ दिया।

लेकिन यदि आप इसे यूआई थ्रेड पर चला रहे हैं और वेब सर्वर को जवाब देने के लिए कुछ सेकंड से अधिक समय लगता है तो आपको सिस्टम से चेतावनी का जवाब नहीं दे रहा है। सुनिश्चित करें कि आप एक अलग थ्रेड पर कोई नेटवर्क स्थानान्तरण करते हैं।

+0

हाँ, मेरे पास है, धन्यवाद। –

0

यह सुनिश्चित करने के लिए कहने के लिए है, तो आप हमें बता नहीं है क्या त्रुटि है मुश्किल है

3

आप इस तरह से हो सकता है `response` चर उपरोक्त परिदृश्य में के रूप में URLConnection वर्ग

String error = ""; // string field 
private String getDataFromUrl(String demoIdUrl) { 

    String result = null; 
    int resCode; 
    InputStream in; 
    try { 
     URL url = new URL(demoIdUrl); 
     URLConnection urlConn = url.openConnection(); 

     HttpsURLConnection httpsConn = (HttpsURLConnection) urlConn; 
     httpsConn.setAllowUserInteraction(false); 
     httpsConn.setInstanceFollowRedirects(true); 
     httpsConn.setRequestMethod("GET"); 
     httpsConn.connect(); 
     resCode = httpsConn.getResponseCode(); 

     if (resCode == HttpURLConnection.HTTP_OK) { 
      in = httpsConn.getInputStream(); 

      BufferedReader reader = new BufferedReader(new InputStreamReader(
        in, "iso-8859-1"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line).append("\n"); 
      } 
      in.close(); 
      result = sb.toString(); 
     } else { 
      error += resCode; 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return result; 
} 
+0

java.lang.ClassCastException: com.android.okhttp.internal.http.HttpURLConnectionImpl को javax.net.ssl.HttpsURLConnection – beetree

+0

पर नहीं डाला जा सकता है ओह वाह, असफल। मैं "http" url का अनुरोध करने का प्रयास कर रहा था। बहुत अच्छी तरह से ... – beetree

+0

यदि मैं इस कोड में 'HttpsURLConnection' के बजाय' HttpURLConnection' का उपयोग करता हूं, तो क्या अंतर होगा? 'HttpsURLConnection'' HttpURLConnection' से विरासत में मिलता है, उसी उदाहरण से वही विधियों को ' कनेक्ट करें 'और' .getInputStream'। क्या मैं गलत हूं? –

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