उदाहरण मैं नीचे पोस्ट किया है एक उदाहरण है कि मैं 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 जो तुम सब कुछ आप की जरूरत देता है का उपयोग कर सकते अपनी आवश्यकताओं
यदि आप पोस्ट करते हैं तो आपके प्रयास में त्रुटि क्या हो सकती है परिणाम हो सकता है। – Sree
मैं एप्लिकेशन क्योंकि 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(); } –
आपको यह पता लगाना होगा कि आप – Sree