2015-06-19 12 views
9

मुझे सिंक या Async HTTP पोस्ट की आवश्यकता है/वेब-सेवा से HTML डेटा प्राप्त करने के लिए प्राप्त करें। मैं इस पूरे इंटरनेट को खोजता हूं लेकिन मैं अच्छा परिणाम नहीं दे सकता।सिंक या Async HTTP पोस्ट कैसे करें/

मैं इस उदाहरण का उपयोग करने की कोशिश की:

लेकिन उनमें से कुछ भी नहीं है मेरे लिए काम कर।

HttpClient और HttpGet से पता लगा है, त्रुटि है:

"org.apache.http.client.HttpClient is deprecated "

कोड:

try 
{ 
    HttpClient client = new DefaultHttpClient(); 
    String getURL = "google.com"; 
    HttpGet get = new HttpGet(getURL); 
    HttpResponse responseGet = client.execute(get); 
    HttpEntity resEntityGet = responseGet.getEntity(); 
    if (resEntityGet != null) 
    { 
     //do something with the response 
    } 
} 
catch (Exception e) 
{ 
    e.printStackTrace(); 
} 
+0

यदि आप पोस्ट करते हैं तो आपके प्रयास में त्रुटि क्या हो सकती है परिणाम हो सकता है। – Sree

+0

मैं एप्लिकेशन क्योंकि HttpClient और HttpGet से पता लगा है नहीं चलाया जा सकता, त्रुटि है: "org.apache.http.client.HttpClient अब मान्य नहीं है" कोड: कोशिश { HttpClient ग्राहक = नए DefaultHttpClient(); स्ट्रिंग getURL = "http://www.google.com"; HttpGet = new HttpGet (getURL) प्राप्त करें; HttpResponse प्रतिक्रियाGet = client.execute (प्राप्त करें); HttpEntity resEntityGet = responseGet.getEntity(); अगर (resEntityGet = अशक्त!) { // प्रतिक्रिया साथ कुछ करना} } पकड़ (अपवाद ई) { e.printStackTrace(); } –

+0

आपको यह पता लगाना होगा कि आप – Sree

उत्तर

7

उदाहरण मैं नीचे पोस्ट किया है एक उदाहरण है कि मैं Android डेवलपर डॉक्स पर पाया पर आधारित है। आप उस उदाहरण को HERE पा सकते हैं, इसे और अधिक व्यापक उदाहरण के लिए देखें।

आप निम्न

import android.app.Activity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.Toast; 

import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.Reader; 
import java.io.UnsupportedEncodingException; 
import java.net.HttpURLConnection; 
import java.net.URL; 

public class MainActivity extends Activity { 
    private static final String TAG = MainActivity.class.getSimpleName(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     new DownloadTask().execute("http://www.google.com/"); 
    } 

    private class DownloadTask extends AsyncTask<String, Void, String> { 

     @Override 
     protected String doInBackground(String... params) { 
      //do your request in here so that you don't interrupt the UI thread 
      try { 
       return downloadContent(params[0]); 
      } catch (IOException e) { 
       return "Unable to retrieve data. URL may be invalid."; 
      } 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      //Here you are done with the task 
      Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show(); 
     } 
    } 

    private String downloadContent(String myurl) throws IOException { 
     InputStream is = null; 
     int length = 500; 

     try { 
      URL url = new URL(myurl); 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      conn.setReadTimeout(10000 /* milliseconds */); 
      conn.setConnectTimeout(15000 /* milliseconds */); 
      conn.setRequestMethod("GET"); 
      conn.setDoInput(true); 
      conn.connect(); 
      int response = conn.getResponseCode(); 
      Log.d(TAG, "The response is: " + response); 
      is = conn.getInputStream(); 

      // Convert the InputStream into a string 
      String contentAsString = convertInputStreamToString(is, length); 
      return contentAsString; 
     } finally { 
      if (is != null) { 
       is.close(); 
      } 
     } 
    } 

    public String convertInputStreamToString(InputStream stream, int length) throws IOException, UnsupportedEncodingException { 
     Reader reader = null; 
     reader = new InputStreamReader(stream, "UTF-8"); 
     char[] buffer = new char[length]; 
     reader.read(buffer); 
     return new String(buffer); 
    } 
} 

के साथ किसी भी http अनुरोध करने के लिए आप आप Volley जो तुम सब कुछ आप की जरूरत देता है का उपयोग कर सकते अपनी आवश्यकताओं

+0

कंपिलेटर पहचान नहीं है: HttpURLConnection, यूआरएल, ओपनकनेक्शन() और इत्यादि छवि देखें: http://postimg.org/image/sjaav97yx/ –

+1

मैं जो कोड – Neil

+0

कोड में जोड़ा गया आयात जोड़ें, मैंने लाइब्रेरी जोड़ा लेकिन अभी भी कुछ त्रुटियां हैं, स्क्रीन देखें: http : //postimg.org/image/re791wksx/ –

3

सूट करने के लिए कोड के साथ चारों ओर खेल सकते हैं सक्षम हो जाएगा। यदि आप AsyncTask का उपयोग करने और इसे स्वयं प्रोग्राम करने का निर्णय लेते हैं, तो मैं आपकी गतिविधि के अंदर AsyncTask नहीं होने की अनुशंसा करता हूं, बल्कि इसे एक रैपर वर्ग में रखता हूं और उस पर कॉलबैक का उपयोग करता हूं। यह आपकी गतिविधि को साफ रखता है और नेटवर्क कोड को पुन: प्रयोज्य बनाता है। वॉली में उन्होंने जो किया वह कम या ज्यादा है।

+1

वॉली एक शानदार विकल्प है, और वास्तव में एक बेहतर विकल्प है! – Neil

0
**Async POST & GET request** 

public class FetchFromServerTask extends AsyncTask<String, Void, String> { 
    private FetchFromServerUser user; 
    private int id; 

    public FetchFromServerTask(FetchFromServerUser user, int id) { 
     this.user = user; 
     this.id = id; 
    } 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     user.onPreFetch(); 
    } 

    @Override 
    protected String doInBackground(String... params) { 

     URL urlCould; 
     HttpURLConnection connection; 
     InputStream inputStream = null; 
     try { 
      String url = params[0]; 
      urlCould = new URL(url); 
      connection = (HttpURLConnection) urlCould.openConnection(); 
      connection.setConnectTimeout(30000); 
      connection.setReadTimeout(30000); 
      connection.setRequestMethod("GET"); 
      connection.connect(); 

      inputStream = connection.getInputStream(); 

     } catch (MalformedURLException MEx){ 

     } catch (IOException IOEx){ 
      Log.e("Utils", "HTTP failed to fetch data"); 
      return null; 
     } 
     BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); 
     StringBuilder sb = new StringBuilder(); 
     String line; 
     try { 
      while ((line = reader.readLine()) != null) { 
       sb.append(line).append("\n"); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       inputStream.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return sb.toString(); 
    } 

    protected void onPostExecute(String string) { 

     //Do your own implementation 
    } 
} 


****---------------------------------------------------------------*** 


You can use GET request inn any class like this: 
new FetchFromServerTask(this, 0).execute(/*Your url*/); 

****---------------------------------------------------------------*** 

पोस्ट अनुरोध के लिए बस बदलें: connection.setRequestMethod ("GET");

connection.setRequestMethod ("POST");

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